Unable to locate table element generated via ajax using Webdriver - ajax

I am new to Webdriver, I can not let you know the site name and credentials as this is an ongoing project of my company.
I am getting stuck a place, in a page of that website, there is a table generated via ajax on runtime and all data inside it also generated on runtime, there is an element inside that table with xpath as html/body/main/section[2]/div/div/div[2]/table/tbody/tr[1]/td[1]/a, but when I tried to find this element using webdriver, webdriver is unable to locate the element and I am getting an error message as
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"html/body/main/section[2]/div/div/div[2]/table/tbody/tr[1]/td[1]/a"}
Command duration or timeout: 50.10 seconds
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=14.0.1, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=false, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}]
My code for this is
Assert.assertEquals(FrameWorkBase.driver.findElement(By.xpath("html/body/main/section[2]/div/div/div[2]/table/tbody/tr[1]/td[1]/a")).getText(),24514);
When I entered the above xpath in firebug, it is locating the element, means xpath is correct.
Please help me out.
Thanks

Couple of things :
1. Try having an explicit wait on your element, something to the effect..call this method for your elements.. read more
public WebElement waitForElementToBeVisible(WebDriver driver, By by){
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(by));
return element;
}
Your xpath html/body/main/section[2]/div/div/div[2]/table/tbody/tr[1]/td[1]/a looks a bit brittle since it's absolute. Try finding a unique identifier (an id would be best) for one of the elements and then find your way to your element.

If the table is ajax-generated, perhaps you should implement some sort of waitForElement instead of a simple findElement? In my tests I have written a method that waits until jQuery is ready, and it works quite nice. I suspect the reason for your exception is that the element simply isn't present when you are trying to find it.
You could also try first to get only a higher-level element and then work your way deeper. That way you can get a clue what element is missing and see what kind of wait is needed.

Related

what might be the reason for selenium working for xpath sometimes but sometimes fail to identify the xpath?

def linkdin_login(company_name,username,password):
driver.get('https://linkedin.com/')
driver.find_element(By.XPATH,'//*[#id="session_key"]').send_keys(username)
driver.find_element(By.XPATH,'//*[#id="session_password"]').send_keys(password)
driver.find_element(By.XPATH,"//button[#class='sign-in-form__submit-button']").click()
#def company_info(company_name):
element = driver.find_element(By.CSS_SELECTOR,"#global-nav-typeahead > input")
element.send_keys(company_name)
element.send_keys(Keys.ENTER)
driver.implicitly_wait(10) # seconds
driver.get(driver.find_element(By.CSS_SELECTOR,".search-nec__hero-kcard-v2 > a:nth-child(1)").get_attribute("href"))
driver.implicitly_wait(10)
people()
by the above code i am logging into LinkedIn and fetching the LinkedIn page of the some companies after getting the page I am trying to get the employee data by using people function show below
def people():
driver.implicitly_wait(10)
driver.get(driver.find_element(By.XPATH,"/html/body/div[5]/div[3]/div/div[2]/div/div[2]/main/div[1]/section/div/div[2]/div[1]/div[2]/div/a").get_attribute("href"))
driver.implicitly_wait(10)
people = driver.find_element(By.XPATH,"/html/body/div[4]/div[3]/div[2]/div/div[1]/main/div/div/div[2]/div/ul")
people_data = people.find_elements(By.TAG_NAME,"li")
for i in people_data:
print(i.text)
in this function i am trying to access the link to employees data
that is where the problem lies
the line 2 of people function i trying to get the link the problem is due to some reason sometimes i am getting the link(not to frequently!!) but most of the time i am getting the error saying Xpath not found
i didn't know how to attach a html page so i am attaching the link
([https://www.linkedin.com/company/google/](https://www.stackoverflow.com/)
1. I tried implicit wait assuming that the program is trying to access the Xpath during loading of the page

Selenium-webdriver in Ruby crashes when element is not found

When looking at a dynamic element on a webpage, Selenium crashes if the element is not present. As a result I'm having to rescue the application to continue. I figure I'm doing something wrong with the syntax
response = driver.find_element(:class, element).text
If the element is not found, Selenium errors and crashes the application. This happens regardless of my browser configuration.
Selenium is not crashing. Your code has encountered an exceptional condition (attempting to work with an element that is not there). The code is correctly responding to that exceptional condition by raising a NoSuchElementError.
If you are trying to determine if an element is there, you can use Driver#find_elements and check if the Array#size equals 0.
If you are trying to work with an element that is not yet on the page, you'll need to create an explicit wait to poll for the element to show up as in DebanjanB's answer.
You need to wait a bit for the element to be visible before you try to locate it as follows:
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
ele = wait.until { driver.find_element(:class, element).displayed? }
response = ele.text

Wait until element has disappeared in Cypress

please, tell me what methods to use to wait for a loading screen element to disappear? PS. I'm not using an API request.
I tried to use two methods, but it doesn't work properly:
1. cy.get('#loading', { timeout: 30000 }).should('not.be.visible');
I get the error: -- Timed out retrying after 30000ms: Expected to find element: #loading, but never found it.
2. Used plugin (cypress-wait-until) like so cy.waitUntil(() => {document.querySelector('#loading') === null};
This approach doesn't find the element at all.
If you are trying to retrieve an element that is not in the DOM, use not.exist instead:
cy.get('#loading').should('not.exist');
In cases where you do need to wait, you can try using cy.wait:
An example use case for this might be if Cypress has to route to your page first and you want to ensure the page loads before you start testing:
cy.wait(200);

Appium XPath Selector returns WebDriverException

I'm attempting to perform some automation of an Android app, using NGTest framework and UIAutomator.
The below is output from UI Automator - my XPath knowledge is not the hottest but I've attempted to select the parent of the Camera button using the following Java code:
WebElement e = driver.findElement(By.xpath("//android.widget.LinearLayout[#resource-id=com.instagram.android:id/tab_bar']"));
However this returns an exception:
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command.
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z'
Could anyone guide me better on how to use XPath expressions to drill down into the tree from UIInspector? Should I be starting at the top and working my way down, or is it ok to try and grab children in this way?
I have also tried grabbing the camera icon directly using the bounds, but that didn't work either.
Attempted:
WebElement abc = driver.findElement(By.xpath ("//android.widget.ImageView[contains(#bounds,'[508,2119][571,2131]')']"));
abc.click();
I think you are missing a quote sign at the beginning of your resource-id value.
You are using:
WebElement e = driver.findElement(By.xpath("//android.widget.LinearLayout[#resource-id=com.instagram.android:id/tab_bar']"));
And it should be:
WebElement e = driver.findElement(By.xpath("//android.widget.LinearLayout[#resource-id= 'com.instagram.android:id/tab_bar']"));
This element explorer can really help you locating the elements

HP UFT - WebElement cannot be found even after using correct xpath

I am new to UFT. I have a long "complex" xpath that finds precisely one element in chrome browser developer tools. When I use the same xpath in uft, the tool complains that the xpath is not in the object repository. Why does this happen and how do I fix it ?
This is what the xpath looks like:
//div[#class='a b c']//div[#class='p-q r-s']//div[#class='m n']//button[contains(text(), 'yes')]
I have to use such complex xpath because there are no ID attributes in this part of the page or any other 1-2 attributes which can uniquely identify the element.
Please help.
EDIT: My Vbscript code looks similar to this code:
Dim aButtonLoc
aButtonLoc = "//div[#class='a-b c-d-e g']" & _
"//div[#class='p-q r-s-t']//div[#class='uv w-x']" & _
"//button[contains(text(), 'Yes')]"
Error message: The {full xpath here} object was not found in the Object Repository. Check the Object Repository to confirm that the object exists or to find the correct name for the object.
There is an error in my aButtonLoc locator. It should have xpath mentioned, like this: aButtonLoc = "xpath:=//d...etc."

Resources