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,080 points
142 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,498 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
11,126 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
451 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
419 views
0 votes
1 answer

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,510 points
3,103 views
0 votes
1 answer
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,349 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,890 points
2,497 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
25,724 views
+2 votes
2 answers

How can we handle authentication popup in Selenium WebDriver using Java

You can use below code for selenium ...READ MORE

answered Jun 21 in Selenium by TEJAS KIRAN RATHI
16,312 views