0

I'm trying to click in a radio button by it's Xpath or Id. I inspected the element with Google Developer Tool and was able to get below details:

  1. Element Name: <label for="1346_Voltagem_0" class="dimension-Voltagem espec_0 skuespec_110v skuespec_Voltagem_opcao_110V skuespec_Voltagem_opcao_110v">110V</label>

  2. Xpath: /html/body/div[9]/div/div/div[3]/div[3]/div[1]/div/ul/li[2]/span/label[1]

I would like to know how to say to the code that it has to click on that radiobutton?

Click here for the page where I want to click the radioButton. Also, please refer the below screenshot for the exact element

actual image

Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39

4 Answers4

0

Avoid using absolute xpaths as a small change in the UI will cause a lot of changes to be made in your code. For your question, the following xpaths will work.

driver.findElement(By.xpath("//label[text()='110V']")).click();

driver.findElement(By.xpath("//label[contains(@class,'110v')]")).click();
0

Try this:

driver.findElement(By.xpath("//input[@class='skuselector-specification-label input-dimension-Voltagem sku-selector skuespec_110v change-image']")).click();
-1

You should use xpath for this since you need to find the label and then find the previous input to it

You can find the label using

//div[contains(@class,'prod-sku-selector')]//label[.='110V']

and now to get the previous input you can use

//div[contains(@class,'prod-sku-selector')]//label[.='110V']/preceding-sibling::input[1]

css selector

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
-1

I always believe that xpath should not be much dependent on your html divs and hierarchy as that is very easy to fail even with a smallest change. So, using xPath like "/html/body/div[9]/div/div/div[3]/div[3]/div[1]/div/ul/li[2]/span/label[1]" is not a good option.

In this case, I would suggest you to go with below xpath:

If first radio button needs to be clicked:

//div[contains(@class, 'prod-sku-selector')]//input[@type='radio'][1]

If radio button with value 110v needs to be clicked:

//div[contains(@class, 'prod-sku-selector')]//input[contains(@value, '110V')]

Then you can get element using :

radioBtn1 = findElement(By.xpath("//div[contains(@class, 'prod-sku-selector')]//input[@type='radio'][0]"));

Also, you can click radio in selenium using:

radioBtn1.click();
Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39
  • The xpath isn't correct. `[0]` is not a valid index. It should start from `[1]`. Also you should try to identify control using some reference and not just indexes. Because if `110V` option is taken off then this would just blindly select the `220V` one without raising an issue – Tarun Lalwani Mar 26 '18 at 17:54
  • Please see [this](https://i.stack.imgur.com/EMhn7.png). The xpath `//div[contains(@class, 'prod-sku-selector')]//input[@type='radio'][0]` you listed earlier doesn't work – Tarun Lalwani Mar 26 '18 at 18:02
  • I have updated the index in my answer. For the second point related to 110v, that totally depends upon the requirement, if first radio button needs to be clicked, the above option will work or else if specific 110v needs to be clicked, the below xpath can be used: //div[contains(@class, 'prod-sku-selector')]//input[contains(@value, '110V')]. The whole motive of this platform is to help the people and not spoon feed them – Aman Chhabra Mar 26 '18 at 18:07