TheDemonicSky
TheDemonicSky

Reputation: 33

Selenium element could not be scrolled into view

What I am trying to do here is get this script to check a "I have read and agree to the terms and services" checkbox as shown in the photo the highlighted is the html for the checkbox itself. enter image description here

This is the code I am using in order to locate and click on the checkbox

tos_checkbox = browser.find_element_by_xpath("//input[@id='mat-checkbox-1-input']")
tos_checkbox.click()

When I run the script I get an error returned saying

selenium.common.exceptions.ElementNotInteractableException: Message: Element <input id="mat-checkbox-1-input" class="mat-checkbox-input cdk-visually-hidden" type="checkbox"> could not be scrolled into view

Any ideas on how to fix?

Here is a copy of the full script so far if that is necessary info

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep

browser = webdriver.Firefox()
browser.implicitly_wait(5)

browser.get('https://onlinebusiness.icbc.com/webdeas-ui/login;type=driver')

lastname_input = browser.find_element_by_xpath("//input[@id='mat-input-0']")
licensenumber_input = browser.find_element_by_xpath("//input[@id='mat-input-1']")
keyword_input = browser.find_element_by_xpath("//input[@id='mat-input-2']")

lastname_input.send_keys("<lastname>")
licensenumber_input.send_keys("<licensenumber>")
keyword_input.send_keys("<keyword>")

tos_checkbox = browser.find_element_by_xpath("//input[@id='mat-checkbox-1-input']")
tos_checkbox.click()

login_button = browser.find_element_by_xpath("//button[@class='mat-raised-button']")
login_button.click()

sleep(5)

browser.close()

Upvotes: 1

Views: 4635

Answers (3)

Justin Lambert
Justin Lambert

Reputation: 978

 JavascriptExecutor js = (JavascriptExecutor) driver;

    //Launch the application        
    driver.get("url here");

    //Find element by link text and store in variable "Element"             
    WebElement Element = driver.findElement(By.xpath(" "));

    //This will scroll the page till the element is found       
    js.executeScript("arguments[0].scrollIntoView();", Element);

Upvotes: -1

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

Use the following.

wait = WebDriverWait(browser, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#mat-checkbox-1 > label > div"))).click()

Import

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

Upvotes: 0

Jonathan PETIT
Jonathan PETIT

Reputation: 196

Just click on the parent element (with './..') and it'll work fine:

tos_checkbox = brower.find_element_by_xpath("//input[@id='mat-checkbox-1-input']/./..")
tos_checkbox.click()

Regards !

Upvotes: 4

Related Questions