【问题标题】:Element not interactable selenium [duplicate]元素不可交互的硒[重复]
【发布时间】:2019-09-30 19:46:22
【问题描述】:

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

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)

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    经过大量搜索,我找到了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 强制单击按钮...

    【讨论】:

    • 这是因为元素不能被真实用户交互,selenium 被用来模拟真实用户浏览网站,但是 javascript 会忽略 selenium 所做的这些断言(可见,可交互,可点击,等...)并直接调用元素的点击事件。
    • @Valga 是的,我了解硒的机制,但记下它们是个好主意!我尝试了等待条件以使其可见,但已超时(20 秒等待时间)。我似乎也找不到任何可以在 HTML 中覆盖它的东西......在我看来,唯一的解决方案是在它位于某个其他元素的“下方”时强制单击它,但如果你知道另一个解决方案,我很想看到你发布它并给我发送一个 ping 让我看看它!
    • @Reedinationer,谢谢你的朋友。这对我有用,但你能解释一下这是如何工作的吗?
    • @Number70 我建议您查看this post,它解释了单击.click() 和单击此答案使用的javascript 之间的区别。希望有帮助!
    • @Reedinationer,谢谢,我会检查一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 2022-01-15
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多