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
252 views
in Technique[技术] by (71.8m points)

python - Selenium not clicking on webelements sometimes

This is the code I have that works about 80 percent of the time. But sometimes what happens is that the script hovers to the first element, moves to the drop down menu element and then proceeds to not click.

However, the script seems to think it has clicked the element and the print message shows up with no exception occurring. Is there anyway I can write some code to confirm the button has been clicked, and if it is not clicked keep trying to click it?

#click on sourcekey->view tender
time.sleep(20)
sourcekey = WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Source Key']")))
view_tender = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//a[text()='View RFQ/Tender/RFP'] ")))
ActionChains(driver).move_to_element(sourcekey).double_click(view_tender).perform()   
print("Click on view tender")

question from:https://stackoverflow.com/questions/65557620/selenium-not-clicking-on-webelements-sometimes

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

1 Reply

0 votes
by (71.8m points)
actions = ActionChains(driver)

sourcekey = WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Source Key']")))      
    
actions.move_to_element(sourcekey).perform()
    
view_tender = WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH,"//a[text()='View RFQ/Tender/RFP'] ")))
actions.double_click(view_tender).perform()   

print("Click on view tender")

Try using visibility of element than presence, the action class might try to click the element before it is visible


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

...