Using XPath with Selenium-Java code

0 votes

Below is the HTML code I am testing and I have to click on the option having text "Auto". 

<select name="category" id="category">
    <option value="0">&laquo;Seleziona la categoria&raquo;</option>
    <option value='1' style='background-color:#ddd' disabled="disabled" id='cat1' >-- VEICOLI --</option>
    <option value='2'  id='cat2' >Auto</option>
</select>

I've tried all the possible codes below.

d.findElement(By.xpath("/select[@id=category]/option[@id=cat2]")).click();

d.findElement(By.xpath("/select[@id=category]/option[text()='Auto']")).click();

d.findElement(By.xpath("//select[@id=category]/option[Auto]")).click();

But still I get no such element exception when tries with all these XPaths.

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"/select[@id=category]/option[@id=cat2]"}

Which of these is the correct syntax? can someone help me? Help plz!

Apr 13, 2018 in Selenium by kappa3010
• 2,090 points
186 views

1 answer to this question.

0 votes

There is a minor syntax error in each of your XPaths statements. Your missing to include quotes. Below is the correct syntax.

d.findElement(By.xpath("//select[@id='category']/option[@id='cat2']")).click();
answered Apr 13, 2018 by king_kenny
• 3,710 points

Related Questions In Selenium

0 votes
1 answer

Firefox browser not opening with given URL using Selenium Webdriver with Java

Download the latest selenium jar and replace ...READ MORE

answered Apr 30, 2018 in Selenium by Samarpit
• 5,890 points
4,759 views
0 votes
1 answer

How to get next sibling element using XPath and Selenium for Java?

Below code will help you: Try following-sibling axis : WebElement ...READ MORE

answered May 15, 2018 in Selenium by Samarpit
• 5,890 points
12,727 views
0 votes
1 answer

Capturing JavaScript errors with Selenium WebDriver using Java

There is logs Beta version in WebDriver driver.manage().logs().get(LogType.BROWSER); Console output will be ...READ MORE

answered May 18, 2018 in Selenium by Samarpit
• 5,890 points
560 views
0 votes
1 answer

Identifying the page iframe in Selenium using Java Code

Open the page in a browser and ...READ MORE

answered May 24, 2018 in Selenium by Samarpit
• 5,890 points
523 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,520 points
3,769 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,749 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
2,729 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
3,092 views
+4 votes
2 answers

Can we get the HTTP Response Code in Selenium with Java?

It is indeed possible to get http ...READ MORE

answered Apr 20, 2018 in Selenium by king_kenny
• 3,710 points
27,949 views
+2 votes
3 answers

How can we handle authentication popup in Selenium WebDriver using Java

1) By passing user credentials in URL. String ...READ MORE

answered Aug 30 in Selenium by Sri
• 2,970 points
18,305 views