简体   繁体   English

If Statements with Selenium using find element by xpath(包含文本)

[英]If Statements with Selenium using find element by xpath (contains text)

I am trying to find an element by text and depending if it is found or not print an output to the screen我正在尝试通过文本查找元素,并根据是否找到它在屏幕上打印 output

This is what I have so far but I just cant get it to work这是我到目前为止所拥有的,但我无法让它工作

if driver.find_element_by_xpath("//*[contains(text(),'addFromWishlist')]").isEmpty()
{
System.out.println("In stock");
}
else{
  System.out.println("Not In Stock");
}

Please use find_elements (plural) so that it would return a list of web element if found , if not it will return empty list .请使用find_elements (plural) ,这样如果找到,它将返回return a list of web element ,如果没有,它将return empty list either way there won't be any exception .无论哪种方式都不会有任何例外

try:
    if len(driver.find_elements(By.XPATH, "//*[contains(text(),'addFromWishlist')]")) >0 :
        print('In stock')
    else:
        print('Not In Stock')
except:
    print('Something went wrong')
    pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM