0

so I wanted to click a checkbox on website using selenium (python).That's the button I want to click

So I thought that it would work with that code: driver.find_element_by_xpath("//input[@name='termsCheck']").click()

But that gives me an errorThat's the error I get

Additional info: there are 2 more checkboxes on the same page which have also <span class="custom-checkbox"> ::before ::after </span>

Has anyone an idea how to get selenium to click the checkbox?

1
  • That's almost impossible to diagnose your problem without seeing actual html. So, add either a link to the site, or its html code. Commented May 25, 2021 at 22:38

3 Answers 3

1

I have seen some scenarios were the element must be clicked with javascript because it is covered by other elements. Alternatively you could click the <span> element that is covering it.

Here is how to click the element with javascript using python and selenium. Since you have not provided the HTML I am assuming that the xpath you provided uniquely identifies the element you want to click.

element_to_click = driver.find_element_by_xpath("//input[@name='termsCheck']")
driver.execute_script("arguments[0].click();", element_to_click )
Sign up to request clarification or add additional context in comments.

1 Comment

tried everything ...but finally found. Thnaks man
0

On most browsers you should be able to copy the XPath or CSS selector by right clicking the specific element on the developer tools console. The click() method should work.

Comments

0

The code is attempting to click the checkbox and Selenium API doesn't like that. The error informs about that, but is not specific enough. Try using auxiliary class Select instead:

from selenium.webdriver.support.ui import Select

element = driver.find_element_by_xpath("//input[@name='termsCheck']")
select = Select(element)
select.select_by_index(index)

Additionally, make sure that XPath //input[@name='termsCheck'] is only matching single element.

Refer to Selenium Python documentation for more details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.