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,020 points
83 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,650 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,130 points
3,225 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,130 points
6,582 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,130 points
246 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,130 points
152 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,110 points
1,007 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,130 points
1,153 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,130 points
892 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,650 points
14,463 views
+2 votes
1 answer

How can we handle authentication popup in Selenium WebDriver using Java

The Alert Method, authenticateUsing() lets us skip ...READ MORE

answered Apr 18, 2018 in Selenium by king_kenny
• 3,650 points
8,831 views