使用Python的Selenium无法点击元素

问题描述 投票:0回答:1

Browser Dev Tools Snip

我正在尝试单击以下按钮,我有以下代码。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

driver.get(
    "SITE")
driver.execute_script(
    "return document.getElementById('attentive_overlay').remove();")
driver.find_element(
    By.ID, "search_bar_input").send_keys("preservation kit")
driver.find_element(
    By.ID, "search_bar_input").send_keys(Keys.ENTER)

atbb = driver.find_element(
    By.CSS_SELECTOR, "[data-test-id='product_add_to_bag']")

我通常会收到此错误。

selenium.common.exceptions.ElementNotInteractableException: Message: Element <button class="buttonstyled__ButtonStyled-sc-1raww7j-0 lmuxzm sc-98482ff5-4 gjMHqx" type="submit"> could not be scrolled into view

我尝试使用此文档来解决:https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html?highlight=move_to_element#selenium。 webdriver.common.action_chains.ActionChains.move_to_element

atbb = driver.find_element(
    By.CSS_SELECTOR, "[data-test-id='product_add_to_bag']")

driver.move_to_element(atbb)

driver.atbb.click()

但我现在收到此错误:

AttributeError: 'WebDriver' object has no attribute 'move_to_element'

但我似乎无法让它继续工作。任何帮助都会很棒!

python selenium-webdriver webdriver
1个回答
0
投票

尝试这样

element = driver.find_element_by_id("my-id")

actions = ActionChains(driver) 
actions.move_to_element(element).perform()

driver.execute_script("arguments[0].scrollIntoView();", element)
© www.soinside.com 2019 - 2024. All rights reserved.