Reputation: 15
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?
Upvotes: 0
Views: 2017
Reputation: 3790
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 )
Upvotes: 1
Reputation: 5082
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.
Upvotes: 0
Reputation: 176
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.
Upvotes: 0