1

I have been testing some new browser automation tools. One of them being Selenium. One thing I am working on is using Python to open a web page. Go to that web page and look for a certain button. If the button says "Yes Button" do nothing and just refresh the page. If the button changes after a refresh to "No Button" then click on that button. I will post the code below. The only thing I left out is my website address. Any help is really appreciated. Currently my script below does the refresh part but when the button changes to "No Button" it just stops and will not click on the button. I am not sure if my while loop is wrong or my understanding of Selenium.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
import time
################import the chrome web driver and define the location###############
PATH = "c:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
###################################################################################

###########open the web page and print the title##############
driver.get("https://mywebsite.com")
print(driver.title)
time.sleep(1)
##############################################################

#Look for search button and wait for it to change to something else. 
while True:
    searchbutton = WebDriverWait(driver, 60).until(expected_conditions.presence_of_element_located((By.LINK_TEXT, "Yes Button")))
    driver.refresh()
else:
    searchbutton = driver.find_element_by_link_text("No Button")
    searchbutton.click()
11
  • Give the actual link Commented Nov 17, 2020 at 15:00
  • It is just a little test website that I built. I put a button on there and that is it. kjustin765.wixsite.com/website Commented Nov 17, 2020 at 15:05
  • @jkotts When does it change to a No button? I refreshed the button..it stays the same? Commented Nov 17, 2020 at 15:09
  • I have to change it manually.I can change it for you if you like. Commented Nov 17, 2020 at 15:34
  • So, you are saying when you change the text label of the button from Yes button to No button it cannot find it? Commented Nov 17, 2020 at 15:35

1 Answer 1

1
else:
    searchbutton = driver.find_element_by_link_text("No Button")
    searchbutton.click()` It never reaches here because `While is always True

The program never reaches this part because it never gets out of While True. It should be something like this. Also Excpected Conditions will make the driver wait for the element to appear failing which, it will give a timeout error. So, we do it like this.

   while True:
    button1 = driver.find_element_by_xpath('//*[@id="comp-khm867e6"]/a/span')
   
    if 'Yes' in button1.text:
        driver.refresh()
        time.sleep(10)
    elif 'No' in button1.text:
        button1.click()
        break
Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.