Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

selenium, please help me about if

Use : Chrome, selenium.

if driver.find_element_by_xpath('AAA') == False:
   WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "BBB"))).click()
   WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "CCC"))).click()
if driver.find_element_by_xpath('AAA') == True:
   WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "CCC"))).click()

How can i do this?

  1. When I can't see AAA, I should click BBB button.
  2. When I can see AAA, I must not to click BBB button.
  3. When I can see AAA + click BBB button, I can't see AAA.
question from:https://stackoverflow.com/questions/65876433/selenium-please-help-me-about-if

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
 elem = driver.find_elements_by_xpath('AAA')
 if not len(elem): 

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "BBB"))).click() 
     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "CCC"))).click()

 else:

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "CCC"))).click()

find element doesn't return true or false , use find_elements intead and check the length . if length is not zero then element exists. so not len(elem) checks it doesn't exist meaning false


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...