【发布时间】:2019-12-25 13:50:09
【问题描述】:
如果在页面上找到元素,我会尝试单击按钮。该元素大部分时间都在页面上。 3次有效,1次无效。 这是我的代码:
elements = driver.find_elements_by_xpath("//h2[contains(text(),'No results found')]")
if (len(elements)>0):
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CLASS_NAME, 'ut-navigation-button-control'))).click()
else:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Buy Now')]"))).click()
以下是我有时会遇到的错误:
ElementClickInterceptedException: Message: Element <button class="ut-navigation-button-control"> is not clickable at point (128,80) because another element <div class="ut-click-shield showing interaction"> obscures it
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Buy Now')]"))).click()
except:
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CLASS_NAME, 'ut-navigation-button-control'))).click()
它的工作原理是这样的,但是在除此之外需要很长时间。有谁知道如何让它快速通过?
【问题讨论】:
-
if (len(elements)>0)不是一种规范的方法,因为我们可以轻松地使用find_element_by_xpath("//h2[contains(text(),'No results found')]")来识别元素。但是,如果element_to_be_clickable((By.CLASS_NAME, 'ut-navigation-button-control'))唯一标识所需元素,您能否确认一次? -
如果您的意思是如果该元素是该类的唯一元素,是的,它是唯一的
-
你能用 XPATH 代替
CLASS_NAME吗? -
喜欢这个?
((By.XPATH, 'ut-navigation-button-control'))? -
我已经编辑了问题。可以看看吗?
标签: python selenium selenium-webdriver webdriver webdriverwait