【问题标题】:The element doesn't load and selenium is not able to click元素未加载且 selenium 无法单击
【发布时间】:2022-08-23 11:00:26
【问题描述】:

如果您阅读了我以前的问题,您会知道,我正在制作动漫刮刀。我尝试抓取 fmbed 但失败了,所以开始抓取原始页面。在这里,我无法单击该元素。编码-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from seleniumwire import webdriver

# Chrome Stuff
chrome_options = Options()
chrome_options.add_argument(\'--no-sandbox\')
# chrome_options.add_argument(\'--headless\')
chrome_options.add_argument(\'--disable-dev-shm-usage\')
driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()

url = \'https://gogoanime.fi/shingeki-no-kyojin-the-final-season-part-2-episode-7\'
driver.get(url)
wait = WebDriverWait(driver, 20)

wait.until(EC.element_to_be_clickable(By.XPATH, \'html/body/div/div/div[3]/div[2]/div[12]/div[1]/div/div/div[2]/div\')).click()

driver.implictly_wait(5)

for request in driver.requests:
    if request.response:
        print(request.url)
        # print(request.response.headers)
driver.quit()
driver.close()

控制台——

Traceback (most recent call last):
  File \"/home/zenitsu/PycharmProjects/anistreamsrc/main.py\", line 20, in <module>
    wait.until(EC.element_to_be_clickable(By.XPATH, \'html/body/div/div/div[3]/div[2]/div[12]/div[1]/div/div/div[2]/div\')).click()
TypeError: element_to_be_clickable() takes 1 positional argument but 2 were given

Process finished with exit code 1

请帮我。我会很高兴听到任何 cmets。

  • 我认为应该是EC.element_to_be_clickable((By.XPATH, \'xpath_goes_here\')) 而不是EC.element_to_be_clickable(By.XPATH, \'xpath_goes_here\')。我猜这两个元素都应该在一个元组中
  • 感谢您的评论和答案。但我认为你们没有读过这个问题。我想点击它,但我无法做到这就是我要问的。请帮忙
  • 未检测到的 Selenium 已在他的回答中清楚地解释了这一点,请查看 it

标签: python selenium selenium-webdriver


【解决方案1】:

你应该这样做wait.until(EC.element_to_be_clickable((By.XPATH, 'html/body/div/div/div[3]/div[2]/div[12]/div[1]/div/div/div[2]/div'))).click()

(By.XPATH, 'html/body/div/div/div[3]/div[2]/div[12]/div[1]/div/div/div[2]/div') 应该作为元组传递,而不是单独的参数

【讨论】:

  • 感谢您的评论和答案。但我认为你们没有阅读这个问题。我想点击它,但我无法做到这就是我要问的。请帮忙
  • 这正是问题所在,EC.element_to_be_clickable(By.XPATH, 'html/body/div/div/div[3]/div[2]/div[12]/div[1]/div/div/div[2]/div') 找不到元素,因为您传递了错误的参数,没有执行点击。错误消息中已明确说明。你真的尝试过代码吗?
  • 我真的试过了。它没有用。
  • 错误信息是什么?
  • 找不到元素。我的问题是如何找到元素检查 url plz。
【解决方案2】:

此错误消息...

    wait.until(EC.element_to_be_clickable(By.XPATH, 'html/body/div/div/div[3]/div[2]/div[12]/div[1]/div/div/div[2]/div')).click()
TypeError: element_to_be_clickable() takes 1 positional argument but 2 were given

...暗示在调用 WebDriverWait 时您使用了错误的格式。

正如@Ghost Ops 在他们的 cmets 中提到的,您需要将两个参数都传递给tuple.如此有效,您的代码行将是:

wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, '//html/body/div/div/div[3]/div[2]/div[12]/div[1]/div/div/div[2]/div'))).click()

【讨论】:

  • 我试过它不起作用。
  • 我的错,现在修复了一个错字。让我知道结果。
  • 不工作。如果可以,请尝试运行代码,然后您将看到该元素未被定位。我猜它是动态的。并感谢您的所有回复。
【解决方案3】:

我也遇到了类似的问题,目前通过time.sleep解决了。

我的猜测是 seleniumwire.webdriver 做了一些未知的补丁,导致 WebDriverWait 失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2016-09-05
    • 2018-05-26
    相关资源
    最近更新 更多