首页 > 解决方案 > Selenium Python 点击​​只有一半的时间

问题描述

但我正在尝试为我的网站编写一个单元测试,该测试贯穿所有链接并在网站正常运行时返回 A ok 或 no go。但是我遇到了无法不断单击站点导航栏中的链接的程序的问题。我已经尝试过多次隐式等待。明确的,预期的条件,但页面加载和一半的时间将单击链接并转到站点的该部分,而另一半程序只是停止并且没有单击任何内容。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains

PATH = "C:\Program Files (x86)\chromedriver.exe"

drive = webdriver.Chrome(PATH)
drive.get("https://www.blackhempfamily.com/")

wait = WebDriverWait(drive, 10)
link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Why Black Hemp?")))
link.click()

标签: pythonseleniumselenium-webdriver

解决方案


将是一个更好的标签使用。

wait.until(EC.element_to_be_clickable((By.XPATH, "//p[text()='Why Black Hemp?']")))

推荐阅读