python 无法将Selenium元素滚动到视图中

h9vpoimq  于 2022-12-21  发布在  Python
关注(0)|答案(3)|浏览(221)

我在这里要做的是让这个脚本检查一个“我已经阅读并同意条款和服务”复选框,如照片中所示,突出显示的是复选框本身的html。

这是我用来查找和单击复选框的代码

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

当我运行脚本时,我收到一个错误返回,显示

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

有什么办法修吗?
这里是一个完整的脚本到目前为止,如果这是必要的信息副本

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()
jei2mxaa

jei2mxaa1#

只需单击父元素(带有'./..'),它就会正常工作:

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

问候!

s4n0splo

s4n0splo2#

使用以下方法。

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

进口

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

ocebsuys3#

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);

相关问题