-1

I want to click a button and I have this info. There is no id, and I only have the following code:

enter image description here

How could I make a findElement using the highlighted info?

I tried with

driver.findElement(By.cssSelector("icon f_checkbox inlblk vtop")).click();
Guy
  • 46,488
  • 10
  • 44
  • 88
VanG
  • 55
  • 1
  • 9

3 Answers3

2

You need to tell the driver that those are classes

driver.findElement(By.cssSelector("[class='icon f_checkbox inlblk vtop']")).click();

Or simplified

driver.findElement(By.cssSelector(".icon.f_checkbox.inlblk.vtop")).click();

If you want to use the for attribute

driver.findElement(By.cssSelector("[for='renderCheckbox1-1']")).click();
Guy
  • 46,488
  • 10
  • 44
  • 88
1

When you use CSS selectors, you need to follow some rules like Here.

For Class in CSS it’s just “.” so instead of

driver.findElement(By.cssSelector("icon f_checkbox inlblk vtop").click();

try (given this is unique class otherwise you probably will need to share bigger part of HTML) below (as Guy suggested).

driver.findElement(By.cssSelector(".icon.f_checkbox.inlblk.vtop").click();
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
Viki
  • 157
  • 1
  • 3
1

There are multiple approaches to send multiple classnames using findElement() and you can use either of the following Locator Strategies:

  • Using and only the classNames as follows:

    driver.find_element_by_css_selector(".icon.f_checkbox.inlblk.vtop")
    
  • Using along with the tagName and the classnames as follows:

    driver.find_element_by_css_selector("label.icon.f_checkbox.inlblk.vtop")
    
  • Using as follows:

    driver.find_element_by_xpath("//label[@class='icon f_checkbox inlblk vtop']")
    

However, as per the HTML you have shared, you may need to club up some of the other attributes to locate the element uniquely with in the DOM Tree and you can use either of the following Locator Strategies:

  • Using along with the tagName and the classnames as follows:

    driver.find_element_by_css_selector("label.icon.f_checkbox.inlblk.vtop[for='renderCheckbox1-1']")
    
  • Using as follows:

    driver.find_element_by_xpath("//label[@class='icon f_checkbox inlblk vtop' and @for='renderCheckbox1-1']")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352