Selenium identifies the button as clickable when it wasn't

China☆狼群 提交于 2020-12-15 05:44:12

问题


I'm having an issue where Selenium is saying a button is clickable even when it's disabled.

I'm using Selenium with a website where you have to select a date first and then time slot from a dropdown list before the "Book" button is clickable and will actually do anything. Before the date and time slot are chosen, the button element is

<div id="pt1:b2" class="x28o xfn p_AFDisabled p_AFTextOnly" style="width:300px;" _afrgrp="0" role="presentation"><a data-afr-fcs="false" class="xfp" aria-disabled="true" role="button"><span class="xfx">Book</span></a></div>

After the date and time slot are chosen, the button becomes

<div id="pt1:b2" class="x28o xfn p_AFTextOnly" style="width:300px;" _afrgrp="0" role="presentation"><a href="#" onclick="this.focus();return false" data-afr-fcs="true" class="xfp" role="button"><span class="xfx">Book</span></a></div>

I'm trying to use this code to wait for the button to be clickable

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, 'pt1:b2')))

But Selenium is saying the button is clickable almost immediately upon the website loading even without a date or time slot chosen and the button being completely greyed out and unclickable. I've tested this by checking the timestamps from after navigating to the url and after waiting for the button to be clickable, and there's almost no delay. I've manually resorted to a try except loop and sleeping in between to be able to click the button successfully, but would rather figure out what's causing this issue. Any ideas?


回答1:


Solved this by just searching for the class attribute to change instead of element_to_be_clickable.

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[class='x28o xfn p_AFTextOnly']")))



回答2:


element_to_be_clickable()

element_to_be_clickable is the expectation for checking an element is visible and enabled such that you can click it. It is defined as:

def element_to_be_clickable(locator):
    """ An Expectation for checking an element is visible and enabled such that
    you can click it."""
    def _predicate(driver):
    element = visibility_of_element_located(locator)(driver)
    if element and element.is_enabled():
        return element
    else:
        return False

    return _predicate
    

Now when the website loads even without the date or time slot chosen, the presence of the value p_AFDisabled for the class attribute determines whether the element is enabled or disabled. Next when you fill in the date or time slot the value p_AFDisabled for the class attribute is removed and the element becomes clickable.

So ideally, to wait for the button to be clickable you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.p_AFTextOnly > a[onclick] > span.xfx"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'p_AFTextOnly')]/a[@onclick]/span[text()='Book']"))).click()
    
  • Note: You have to add the following imports :

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



回答3:


Selenium isclickable checks for isdisplayed and is enabled to check click ability

And isdisplay checks for style attribute but isenabled checks for disabled attribute

Now disable is handle not through html disable attribute in most cases but through javascript and css classes

So clickable condition doesn't work in these cases

https://www.selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html

So in this case the click won't throw an error

https://github.com/SeleniumHQ/selenium/blob/trunk/py/selenium/common/exceptions.py

If you check the exception class you can see exceptions are there only for not visible , click intercepted , and not enabled



来源:https://stackoverflow.com/questions/62683452/selenium-identifies-the-button-as-clickable-when-it-wasnt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!