简体   繁体   中英

Python Selenium dynamic condition

I'm doing automation with selenium but I'm having trouble finding an element. I really thought this is because dynamic elements. Sometimes it displays "You found 0" or "You found 1". Here is the html code image Here is my code right now about it

ele = driver.find_element_by_xpath("//*[@class='swal-text' and (contains(text(),'You found 4'))]").is_displayed()
if (ele):
    print("4 ")
else:
    pass

The error says if it's not found, can you help me guys, please Python 3.9.0

Have you tried finding the element by class name? In your case that would be

ele = driver.find_element_by_class_name('swal-text')

I would suggest a better xpath such as //div[@class='swal-modal']/div[@class='swal-text'] for starters.

the problem is your contains is overly strict. Even this would be "better": ele = driver.find_element_by_xpath("//*[@class='swal-text' and (contains(text(),'You found '))]").is_displayed()

If you could programatically determine the number expected you could inject that like any other string concatinations:

expected_value = 4
ele = driver.find_element_by_xpath("//*[@class='swal-text' and (contains(text(),'You found " + expected_value + "'))]").is_displayed()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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