python - 元素不可交互的 Selenium

标签 python selenium

<分区>

我正在尝试在耐克网上商店查看,但我已经尝试了所有方法并进行了搜索,但找不到答案。

https://www.nike.com/nl/nl/checkout/tunnel

我有:

driver.find_element_by_xpath('//button[@id="qa-guest-checkout-mobile"]').click()

<button id="qa-guest-checkout-mobile" aria-label="Guest Checkout" 
class="ncss-btn-accent u-rounded u-full-width ncss-brand u-uppercase pt3-sm pt2-lg pr5-sm pb3-sm pb2-lg pl5-sm fs14-sm d-lg-h">
<span class="d-sm-ib va-sm-m mr1-sm">Afrekenen als bezoeker</span></button>

我曾尝试使用按 id、css 等查找,但它不起作用。我在这里做错了什么?当我打印元素时,我只得到 1 个元素,然后我没有收到错误。

  File "C:\Users\X\OneDrive\Desktop\SN\NA.py", line 23, in <module>
    test = driver.find_element_by_xpath('//button[@id="qa-guest-checkout-mobile"]').click()
  File "C:\Users\X\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\X\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\X\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\X\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
  (Session info: chrome=74.0.3729.131)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.17134 x86_64)

最佳答案

经过大量搜索,我找到了 this postthis answer并能够编写此代码:

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

browser = webdriver.Chrome()
browser.get('https://www.nike.com/nl/nl/checkout/tunnel')
element = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, "//div/button[@id='qa-guest-checkout-mobile']")))
browser.execute_script("arguments[0].click();", element)

成功点击按钮。出于某种原因,您必须使用 javascript 强制单击按钮...

关于python - 元素不可交互的 Selenium ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56119289/

相关文章:

python - 如何在 Mac 上成功安装 OpenPYXL?

python - Django 测试 - 启动多个可以通信的测试服务器?

python - PyDev 调试器与命令行 django runserver 命令不同

python - Python,安装matplotlib时出错

C# 如何从 NoSuchElementException 获取方法和选择器

java - 如何在运行时从 python 提供 Java 输入?

javascript - 在 Selenium Webdriver 中使用 Ctrl + 单击组合打开一个新选项卡

c# - Selenium Webdriver 返回具有奇怪 alpha 值的背景色

javascript - 如何使用selenium选择没有id的下拉菜单

python - 无法解析网页中不同海报的链接