python - 为什么当元素存在时, Selenium 会超时

标签 python selenium webdriver webdriverwait

我在下面有一个问题代码,其中元素变得可见但未被单击。我尝试了 css 选择器和 xpath。

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


PROXY = "socks5://184.178.172.13:15311"  # IP:PORT or HOST:PORT

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)

driver = webdriver.Chrome(chrome_options=chrome_options)

driver.get('https://nitrogensports.eu/sport/tennis/starting-soon')
wait = WebDriverWait(driver, 30)
table = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="modal-welcome-new-button"]')))
table.click()
table = wait.until(EC.presence_of_element_located((By.XPATH, '//div[class="div.events-result-set"]')))
print("finished")
time.sleep(30)
driver.close()

最佳答案

根据您的问题,元素标识为 (By.XPATH, '//*[@id="modal-welcome-new-button"]')没有被点击。

等待结束并且元素被识别并返回后,在调用 click() 时继续前进方法而不是使用 expected_conditions方法presence_of_element_located您需要使用方法 element_to_be_clickable 如下:

driver.get('https://nitrogensports.eu/sport/tennis/starting-soon')
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='party-button highlightable-button highlighted' and @id='modal-welcome-new-button']"))).click()

关于python - 为什么当元素存在时, Selenium 会超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49972306/

相关文章:

javascript - 无法通过 FirefoxDriver 访问全局变量

python - 获取 Flask 模板上下文中的当前函数名

python - 你能创建一个 python 生成器来以随机顺序提供一个范围内的值而不重复吗?

java - Webelement.click() 在 appium 中给出 java.lang.NullPointerException

java - Appium - 无法实例化 AndroidDriver

java - Selenium 2.20 : how to handle User Identification Request dialog

selenium - 每个测试方法都有新的WebDriver实例吗?

python - 没有名为 PyQt5.sip 的模块

python - 将矩阵中位置低于 0 的所有元素转换为 0 (Python)

python - 如何通过 XPath 查找具有两个可能类名的元素?