I have following xpaths, which should be handled on same way by WebDriver, I need to get text content from these.
//*[#id="dialogMessage"]/div[3]
//*[#id="dialogMessage"]/div[3]/p
//*[#id="dialogMessage"]/div[3]/p/span[2]
I tried to use this code to match all of the above ones.
String result_text = driver.findElement(By.xpath("//*[contains(#value, 'dialogMessage')]")).getText();
Only one of these xpaths is present on page in each page loads. I get following error message:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//id[contains(#value, 'dialogMessage')]"}
Looks like you're trying to get all elements that contain attribute value equals to dialogMessage instead try using this xpath:
//*[contains(#id, 'dialogMessage')]
or
//*[#id='dialogMessage']
Related
Element HTML:
Inbox
What I tried:
driver.findElement(By.cssSelector("a[text='Log out']"));
then
driver.findElement(By.xpath("//a[.='Log out']"));
Element snapshot:
HTML
driver.findElement(By.linkText("Log out"));
Something like that should work, you should provide the page html for a better response.
My response is based on the fact that in your first try you are saying text='Log out'.
findElement in selenium works with locatorStrategies (By.cssSelector/By.linkText...) the one that i used (linkText) search in all the anchor tags that are found in the pages () and analyze the text inside the tag, if the text is matched the driver will find the element.
Let me know if it works, otherwise provide me the web page html snippet.
I've seen the actual screen, you must change Log out with Inbox
driver.findElement(By.linkText("Inbox"));
Given the HTML:
Inbox
You need to take care of a couple of things here as follows:
Within cssSelector doesn't supports the :contains("text") method
Within xpath for exact text matches you need to use text()
Solution
To identify the element you can use either of the following locator strategies:
Using linkText:
WebElement element = driver.findElement(By.linkText("Log out"));
Using cssSelector:
WebElement element = driver.findElement(By.cssSelector("a[href$='INBOX'][title='View the Inbox']"));
Using xpath:
WebElement element = driver.findElement(By.xpath("//a[text()='Inbox' and #title='View the Inbox']"));
I wanted to locate a element of a web page using class name in Selenium. This is the web element that I tried:
<button class="signup-button b green">Get Started!</button>
When I try this way, I was unable to locate the button;
driver.findElement(By.className("signup-button")).click();
But, using css selector like below, it was working;
driver.findElement(By.cssSelector("button.signup-button")).click();
What is the reason for that sometimes working and other times not working?
As you are able to locate the following element:
<button class="signup-button b green">Get Started!</button>
using:
driver.findElement(By.cssSelector("button.signup-button")).click();
but was unable to locate the same element using:
driver.findElement(By.className("signup-button")).click();
That's because there were other elements which renders within the HTML DOM with className as signup-button even before the desired element, which possibly may be invisible by design.
Ideally, you should also be able to use a xpath based Locator Strategy:
driver.findElement(By.xpath("//button[#class='signup-button' and text()='Get Started!']")).click();
Best Practices
But as per best practices, you need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.signup-button"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='signup-button' and text()='Get Started!']"))).click();
References
You can find a couple of relevant discussions in:
NoSuchElementException, Selenium unable to locate element
You need to use relative xpath , if you could not have locators like id and class name
Can you try with //button[contains(text(),"Get Started!")] this xpath
i am trying to display the number 1 in my console, apparently it did not work, any advise please
This is my code apparently it didn't work.
String error1 = driver.findElement(By.xpath("//div[#class='label label-error']/span[#class='sidebar-label']")).getText();
the console error output
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='label label-error']/span[#class='sidebar-label']"}
Your Xpath is Wrong, Use one of these:
//div[#class="sidebar-label"]/span
//div[#class="sidebar-label"]/span[#class="label label-error"]
//*[#class="label label-error"]
Inspect element from Google Chrome:
<div class="form-style-abc">
<fieldset>
<legend><span class="number">*</span>Search Zone</legend>
Need to retrieve the "Search Zone", however im unable to perform the search and getText()
I had performed the following on Eclipse but getting error:
Code:
String HeaderTxt = driver.findElement(By.xpath("//span[#class = 'number']")).getText();
System.out.println(HeaderTxt);
Error Message:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//span[#class = 'number']"}
As per the HTML you have provided to extract the text Search Zone you can use the following line of code :
String HeaderTxt = driver.findElement(By.xpath("//div[#class='form-style-abc']/fieldset/legend[(self::legend) and not (#class='number')]")).getText();
System.out.println(HeaderTxt);
Update 1
As per your comment update as you are still seeing NoSuchElementException possibly you need to wait for the element inducing WebDriverWait as follows :
WebElement HeaderTxtElem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='form-style-abc']/fieldset/legend[(self::legend) and not (#class='number')]")));
System.out.println(HeaderTxtElem.getText());
Update 2
As you mentioned the element is within an <iframe> tag you need to switch to the exact frame and then look out for the WebElement. Here you can find a detailed discussion on How to switch to frame in Selenium Webdriver Java
Update 3
Xpath Explanation
Through the xpath as "//div[#class='form-style-abc']/fieldset/legend" we have reached till the <legend> node. Now you want to get the text Search Zone only. So being within the <legend> tag you have to trim the child <span> tag which have a class as number. So we added a clause within our xpath not to consider (#class='number') while retrieving the text as in :
String HeaderTxt = driver.findElement(By.xpath("//div[#class='form-style-abc']/fieldset/legend[(self::legend) and not (#class='number')]")).getText();
Using TestNG and #Factory annotation
I am trying to click element in drop-down menu, but when I add more than two parameters in DataProvider (test is running 3 times in a row, for example), following error appears in test runner:
org.openqa.selenium.NoSuchElementException: no such element: Unable to
locate element:
{"method":"xpath","selector":"/html/body/div[61]/ul/li[15]/label"}
So first two times xpath was found:
Xpath = /html/body/div[61]/ul/li[15]/label
But the third time it was changed to:
Xpath = /html/body/div[62]/ul/li[15]/label
How can I cover this case using regex in selenium (Java)?
Thanks!