【问题标题】:Selenium Webdriver Python - Check if element is visible/detected/present [duplicate]Selenium Webdriver Python - 检查元素是否可见/检测到/存在[重复]
【发布时间】:2021-03-19 09:58:53
【问题描述】:

我想检查 Selenium Web 驱动程序对于以下 XPath 是否可见/存在/检测到元素:

//*[@data-animate-modal-popup="true"]

是否有任何 Selenium 的函数在该元素可见/存在/检测到时返回 TRUE 或 FALSE?

上次我使用以下 IF - Else

phone_number_invalid = driver.find_element_by_xpath('//*[@data-animate-modal-popup="true"]')
if phone_number_invalid:
     code here .....

find_element_by_xpath 在找不到元素时总是抛出错误。我只想在元素可见/存在/检测到时获得 TRUE 或 FALSE。

谢谢。

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    没有原生元素来检查是否存在您可以使用的元素:

    创建一个函数:

    public boolean isElementPresent(By by){
            try{
                driver.findElement(by);
                return true;
            }
            catch(NoSuchElementException e){
                return false;
            }
        }
    

    现在在你的 if 案例中调用它:

     if(isElementPresent(By.xpath("//div[contains(text(),'Report name already exists. Please enter another name.')]")))
       {
           //code
       }
       else if(isElementPresent(By.xpath("//div//span[contains(text(),'Grid Report saved successfully.')]")))
       {
           //code
       }
    

    第二个选项:

    List<WebElement> element1 = driver.findElement(By.xpath("//div[contains(text(),'Report name already exists. Please enter another name.')]"));
       List<WebElement> element2 = driver.findElement(By.xpath("//div//span[contains(text(),'Grid Report saved successfully.')]"));
    
       if(element1.isEmpty() )
       {
           //first set of code
       }
       else if(element2.isEmpty())
       {
           //second set of code
       }
    

    第二个选项处理更多,因此更推荐第一个选项

    【讨论】:

      【解决方案2】:
      • 为方便起见,您为什么不使用扩展程序。
      • 带镀铬:
      • 使用“Xpath helper”检查元素:

      https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en

      • 使用“Xpath finder”查找元素:

      https://chrome.google.com/webstore/detail/xpath-finder/ihnknokegkbpmofmafnkoadfjkhlogph?hl=en

      【讨论】:

        【解决方案3】:

        对于您的问题,您是否已经找到了该元素,或者您需要在某些操作后检查该元素是否可见?

        1. 如果您已经拥有该元素

        使用元素中的方法is_displayed。 例如:

        # you have located element and assign it to elem
        visibility = elem.is_displayed()
        print(visibility) # will print True or False
        
        1. 如果您需要检查元素在某些操作后是否可见

        使用fluent wait,并用try except包裹它。

        from selenium.common.exceptions import TimeoutException
        
        def check_visible_by_xpath(xpath, timeout=2):
            # Will actively search for the element in 2 seconds.
            # If found, return True and stop waiting.
            # If 2 seconds threshold reaches, return False (cannot find it visible in 2 seconds)
            try:
                element = WebDriverWait(driver, timeout).until(
                    EC.visibility_of_element_located((By.XPATH, xpath)))
                return True
            except TimeoutException:
                return False
        
        
        
        # ...
        # Some actions here
        type_in_phone_number()
        # Now you want to check, if after your previous action, something pops up??
        
        visibility = check_visible_by_xpath('//*[@data-animate-modal-popup="true"]', 2)
        # check and do something about it
        if visibility:
            action_if_visible()
        else:
            action_if_not_visible()
        

        【讨论】:

          猜你喜欢
          • 2012-06-11
          • 1970-01-01
          • 2018-01-23
          • 2021-05-17
          • 1970-01-01
          • 1970-01-01
          • 2011-09-25
          • 2011-02-14
          • 2012-09-29
          相关资源
          最近更新 更多