Please see below attached the relevant XML code and my java code to locate the element:
Java code:
//Set date range
WebElement parentStartdate = obj.findElement(By.xpath("//[#id=typein_[Parameters].[Parameter 3]]"));
WebElement child=parentStartdate.findElement(By.xpath("//input[#value='1/1/2019']"));
I get the follow error:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //[#id=typein_[Parameters].[Parameter 3]] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//[#id=typein_[Parameters].[Parameter 3]]' is not a valid XPath expression.
Any help appreciated!
The whole xml structure:
If you have only one QueryBox on the webpage then you can select the element by using the following xpath for your case:
WebElement element = driver.findElement(By.xpath("//input[#class='QueryBox']"));
As the error message says, that is not a valid Xpath expression. Select the first element by
WebElement parentStartdate = obj.findElement(By.xpath("//span[#class='TypeInQuerySpan']/input"));
Try this selector:
//div[contains(#class, 'TypeInDiv')]/span[#class="TypeInQuerySpan"]/input[#type='text'][#class='QueryBox'][#value='1/1/2019']
the 3 identifiers on input are not mandatory, you can choose 1 or 2 or leave all 3, you decide.
Related
I am trying to pass an xpath as a parameter(redirectButton):
WebElement zoomLogo = driver.findElement(By.xpath(redirectButton));
Line in testsuite.xml looks like this:
<parameter name="redirectButton"
value=""//img[#alt='zoom logo colour long']")" />
But when I try to run the test through the testsuite file using testng it shows the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '"//img[#alt='zoom logo colour long']")' is not a valid XPath expression.
I am sure I have proper xpath because when I paste it directly into the code then everything works fine, but when I try to do it via parameter its not working.
Is it possible to do it by parameters?
This error message...
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '"//img[#alt='zoom logo colour long']")' is not a valid XPath expression.
...implies that the xpath which you have used was not a valid XPath expression.
Seems you were almost there. You need to remove the two " characters and the extra ) to make it a valid xpath expression as follows:
<parameter name="redirectButton" value="//img[#alt='zoom logo colour long']" />
References
You can find a couple of relevant detailed discussions in:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//img[contains('1236548597')]' is not a valid XPath expression
selenium - Failed to execute 'evaluate' on 'Document': The string is not a valid XPath expression
Python Selenium SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div contains(#class, 'product-card__subtitle')
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"]
I would like to clik one button, as of now I'm using a XPATH, but sometime the page is changed and I've to update the XPATH.
I'm checking a different way (search and click a image like play.png, click by text, etc.), in order to obtain same result.
Following the portion of the HTML code:
<button class="btn btn-green false">PLAY</button>
EDIT:
I've tried with the code
driver.findElement(By.xpath("//*[contains(text(),’PLAY’]")).click();
but it goes in exception
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //*[contains(text(),’PLAY’] (tried for 10 second(s) with 500 milliseconds interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
at seleniumTest.TestSelenium.play(TestSelenium.java:300)
at seleniumTest.TestSelenium.open_url(TestSelenium.java:277)
at seleniumTest.TestSelenium.seleniumTest(TestSelenium.java:108)
at seleniumTest.TestSelenium.main(TestSelenium.java:85)
Caused by: org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //*[contains(text(),’PLAY’] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(text(),’PLAY’]' is not a valid XPath expression.
You can also use contains or starts-with() when the values of the attributes are dynamic i.e. changing like:
//*[contains(text(),’PLAY’)]
You have to find something that doesn't change.
For example you could do that in XPath using wildcard:
//button[text()='PLAY']
Also you could use the other ways to identify elements in Selenium.
For example By.id(...) if your elements have a unique id.
See Selenium doc for class By
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']
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();