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 14, 2018 in Selenium by kappa3010
• 2,090 points
799 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 14, 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,910 points
7,342 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,910 points
20,326 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,910 points
1,557 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,910 points
1,574 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,500 points
12,717 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,612 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,910 points
6,685 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 3, 2020 in Selenium by Sri
• 3,190 points
7,554 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 21, 2018 in Selenium by king_kenny
• 3,710 points
40,456 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 31, 2020 in Selenium by Sri
• 3,190 points
28,409 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP