I'm trying to click on the checkbox on this website https://echa.europa.eu/information-on-chemicals . For that I use ActionChains library. At first I tried by simply getting the input tag of the checkbox by xpath or css seletor and using click() to click on it but then i got element not interactable error so I triedclick ActionChains to move over to the element and then clicking it but then i getting the following error - MoveTargetOutOfBoundsException: Message: move target out of bounds
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
actions =ActionChains(browser)
WebDriverWait(browser,5)
.until(EC.visibility_of_element_located(('xpath','//input[@id="autocompleteKeywordInput"]')))
termbox = browser.find_element(By.CSS_SELECTOR,'#disclaimerIdCheckbox')
actions.move_to_element(termbox).click(termbox).perform()
inputField = browser.find_element('xpath','//input[@id="autocompleteKeywordInput"]')
typeInput = actions.move_to_element(inputField).click(inputField).send_keys('100-09-4').perform()
WebDriverWait(browser,5)
get_url = browser.current_url
print("The current url is:" str(get_url))```
[1]: https://i.stack.imgur.com/3Cppt.png
CodePudding user response:
In case all you are looking for here is to click the checkbox, the only thing you need to fix is the locator. It is not the input
but label
element who should be clicked.
The below code works
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("--start-maximized")
s = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=s)
url = 'https://echa.europa.eu/information-on-chemicals'
wait = WebDriverWait(driver, 10)
driver.get(url)
wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'disclaimerIdCheckboxLabel'))).click()
CodePudding user response:
To click on the checkbox you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.get('https://echa.europa.eu/information-on-chemicals') # WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='disclaimerIdCheckbox']"))).click()
Using XPATH:
driver.get('https://echa.europa.eu/information-on-chemicals') WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='disclaimerIdCheckbox']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot: