Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium Webdriver Python-检查元素是否可见/检测到/存在_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Selenium Webdriver Python-检查元素是否可见/检测到/存在

Selenium Webdriver Python-检查元素是否可见/检测到/存在,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我想检查以下XPath的Selenium Web驱动程序是否可见/存在/检测到元素: //*[@data-animate-modal-popup="true"] 当元素可见/存在/检测到时,是否有任何Selenium函数返回TRUE或FALSE 上次我使用了下面的IF-Else phone_number_invalid = driver.find_element_by_xpath('//*[@data-animate-modal-popup="true"

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

//*[@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\u by_xpath
总是抛出一个错误。我只想在元素可见/存在/检测时得到TRUE或FALSE


谢谢。

没有本机元素可检查元素是否存在,您可以使用:

创建函数:

public boolean isElementPresent(By by){
        try{
            driver.findElement(by);
            return true;
        }
        catch(NoSuchElementException e){
            return false;
        }
    }
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
   }
现在在您的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
   }
第二选项:

public boolean isElementPresent(By by){
        try{
            driver.findElement(by);
            return true;
        }
        catch(NoSuchElementException e){
            return false;
        }
    }
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
   }
List element1=driver.findElement(By.xpath(“//div[contains(text(),'Report name已经存在。请输入另一个名称')]”);
List element2=driver.findElement(By.xpath(“//div//span[contains(text(),'Grid Report saved successfully.”)]);
if(element1.isEmpty())
{
//第一组代码
}
else if(element2.isEmpty())
{
//第二套代码
}

第二个选项具有更多处理,因此建议使用第一个选项。没有本机元素可检查元素是否存在,您可以使用:

创建函数:

public boolean isElementPresent(By by){
        try{
            driver.findElement(by);
            return true;
        }
        catch(NoSuchElementException e){
            return false;
        }
    }
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
   }
现在在您的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
   }
第二选项:

public boolean isElementPresent(By by){
        try{
            driver.findElement(by);
            return true;
        }
        catch(NoSuchElementException e){
            return false;
        }
    }
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
   }
List element1=driver.findElement(By.xpath(“//div[contains(text(),'Report name已经存在。请输入另一个名称')]”);
List element2=driver.findElement(By.xpath(“//div//span[contains(text(),'Grid Report saved successfully.”)]);
if(element1.isEmpty())
{
//第一组代码
}
else if(element2.isEmpty())
{
//第二套代码
}
第二个选项有更多的处理,因此更推荐第一个选项
  • 你为什么不为了方便而使用分机呢
  • 铬合金:
  • 对检查元素使用“Xpath助手”:
    • 使用“Xpath finder”查找元素:

    • 你为什么不为了方便而使用分机呢
    • 铬合金:
    • 对检查元素使用“Xpath助手”:

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


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

  • 如果您已经拥有该元素
  • 从元素中显示使用方法
    。
    例如:

    #您已找到元素并将其分配给元素
    可见性=元素是否显示()
    打印(可见性)#将打印真或假
    
  • 如果需要检查元素在特定操作后是否可见
  • 使用fluent wait,并使用try except将其包装

    来自selenium.common.exceptions的
    导入TimeoutException
    def check_visible_by_xpath(xpath,timeout=2):
    #将在2秒内主动搜索元素。
    #如果找到,则返回True并停止等待。
    #如果2秒达到阈值,则返回False(在2秒内找不到它)
    尝试:
    element=WebDriverWait(驱动程序,超时)。直到(
    位于((By.XPATH,XPATH))的元素的可见性
    返回真值
    除TimeoutException外:
    返回错误
    # ...
    #这里有一些行动
    在电话号码()中键入
    #现在你想检查一下,在你之前的动作之后,是否有什么东西突然出现??
    可见性=通过xpath检查可见性('/*[@data animate model popup=“true”]”,2)
    #检查一下,做点什么
    如果可见性:
    动作如果可见()
    其他:
    如果不可见,则执行操作()
    
    对于您的问题,您是否已经找到了该元素,或者需要检查在执行某些操作后该元素是否可见

  • 如果您已经拥有该元素
  • 从元素中显示使用方法
    。
    例如:

    #您已找到元素并将其分配给元素
    可见性=元素是否显示()
    打印(可见性)#将打印真或假
    
  • 如果需要检查元素在特定操作后是否可见
  • 使用fluent wait,并使用try except将其包装

    来自selenium.common.exceptions的
    导入TimeoutException
    def check_visible_by_xpath(xpath,timeout=2):
    #将在2秒内主动搜索元素。
    #如果找到,则返回True并停止等待。
    #如果2秒达到阈值,则返回False(在2秒内找不到它)
    尝试:
    element=WebDriverWait(驱动程序,超时)。直到(
    位于((By.XPATH,XPATH))的元素的可见性
    返回真值
    除TimeoutException外:
    返回错误
    # ...
    #这里有一些行动
    在电话号码()中键入
    #现在你想检查一下,在你之前的动作之后,是否有什么东西突然出现??
    可见性=通过xpath检查可见性('/*[@data animate model popup=“true”]”,2)
    #检查一下,做点什么
    如果可见性:
    动作如果可见()
    其他:
    如果不可见,则执行操作()