【问题标题】:Sometime I can click on the button, sometime doesn't work有时我可以单击按钮,有时不起作用
【发布时间】: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)&gt;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


【解决方案1】:

文本为No results found的元素只会在搜索不成功后出现。在成功搜索时,要单击所需元素,您必须为 element_to_be_clickable() 诱导 WebDriverWait,并且您可以使用以下基于 Locator Strategies

try:
    # wait for the visibility of the element with text as "No results found"
    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()='No results found']")))
    # if the element with text as No results found, induce WebDriverWait for invisibilityOfElement obscuring the clickable element
    new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("//div[@class='ut-click-shield showing interaction']")));
    # once the invisibilityOfElement obscuring the clickable element is achieved, click on the desired element inducing WebDriverWait
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='ut-navigation-button-control']"))).click()
except TimeoutException:
    # if search for the element with text as "No results found" raises "TimeoutException" exception click on the element with text as "Buy Now" inducing WebDriverWait
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(,. 'Buy Now')]"))).click()

【讨论】:

  • 我收到此错误...我已经尝试过您的代码,但每次都会收到下一个错误...ElementClickInterceptedException: Message: Element &lt;button class="ut-navigation-button-control"&gt; is not clickable at point (128,80) because another element &lt;div class="ut-click-shield showing interaction"&gt; obscures it
  • @PaulVio 查看更新的答案并告诉我状态。
【解决方案2】:

您可以对元素执行JavaScriptExecutor点击,因为它直接对div执行操作,不受页面上元素位置的影响。
你可以这样做:

button = driver.find_element_by_xpath("//button[contains(text(),'Back')]")
driver.execute_script("arguments[0].click();", button)

【讨论】:

  • 它不起作用。它什么也没做……你有别的想法吗?
  • @PaulVio 您已经在if 条件中添加了此代码,对吗?它显示了一些错误还是什么?
  • 是的,我在 if 条件中添加了它。它没有显示任何错误,它正在工作,但它没有达到我的预期。
  • @PaulVio 你已经发布了另一个我已经回答的问题,所以我从那个答案中选择了 xpath 并编辑了我的这个答案。所以请从这里选择新的答案并尝试一下,让我知道它是否有效
  • 我必须按类名而不是文本来查找元素:(。所以你编辑的那个对我没有帮助
猜你喜欢
  • 1970-01-01
  • 2014-03-24
  • 1970-01-01
  • 2020-01-10
  • 1970-01-01
  • 2017-03-09
  • 1970-01-01
  • 2015-02-02
相关资源
最近更新 更多