I want to click a button and I have this info. There is no id, and I only have the following code:
How could I make a findElement using the highlighted info?
I tried with
driver.findElement(By.cssSelector("icon f_checkbox inlblk vtop")).click();
I want to click a button and I have this info. There is no id, and I only have the following code:
How could I make a findElement using the highlighted info?
I tried with
driver.findElement(By.cssSelector("icon f_checkbox inlblk vtop")).click();
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();
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();
There are multiple approaches to send multiple classnames using findElement() and you can use either of the following Locator Strategies:
Using css-selectors and only the classNames as follows:
driver.find_element_by_css_selector(".icon.f_checkbox.inlblk.vtop")
Using css-selectors along with the tagName and the classnames as follows:
driver.find_element_by_css_selector("label.icon.f_checkbox.inlblk.vtop")
Using xpath 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 css-selectors 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 xpath as follows:
driver.find_element_by_xpath("//label[@class='icon f_checkbox inlblk vtop' and @for='renderCheckbox1-1']")