2

I am trying to programmatically click the button running a script on ubuntu server to get access to the website content. I am trying to reach the site with recipies

This is my code:


driver = webdriver.Firefox(executable_path="/home/ubuntu/.linuxbrew/Cellar/geckodriver/0.26.0/bin/geckodriver",options=options)
data = []


# In[34]:

# click GDPR full-width banner 
start_time = datetime.now()
driver.get("http://some_web_site.here/")
time.sleep(10)

# gdpr_button = driver.find_element_by_link_text("Continue")
#gdpr_button = driver.find_element_by_xpath('//button[text()="Continue"]')

driver.find_element_by_xpath("//input[@style='order:2' and @onclick='sendAndRedirect()']").click()

# //input[@onclick='sendAndRedirect()']
# <button style="order:2" onclick="sendAndRedirect();">Continue</button>

However, i cannot reach the element and get

selenium.common.exceptions.TimeoutException: Message: connection refused

How can I access 'Continue' button in GDPR form? Appreciate your help

sumixam
  • 77
  • 1
  • 8
  • 2
    don't sleep: wait-until for the element you need. https://selenium-python.readthedocs.io/waits.html#explicit-waits – Mike 'Pomax' Kamermans Mar 12 '20 at 20:51
  • @Mike'Pomax'Kamermans yes, this what suggested in the solution below. However, also the problem was in that the website started blocking out my IP. Will read docs fully on weekends but now i just needed one-liner and understand why connection fails. Thanks! – sumixam Mar 12 '20 at 21:40
  • If you're using Selenium for automated testing against not-your-website *respect their robots.txt* - don't automate against it unless you own/control it. Or run the risk of being a terrible user, really =/ – Mike 'Pomax' Kamermans Mar 13 '20 at 00:41

1 Answers1

2

The desired element is a dynamic element so to locate/click() on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick^='sendAndRedirect']"))).click()
    
  • Using XPATH and innerText event:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Continue']"))).click()
    
  • Using XPATH and onclick event:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[starts-with(@onclick, 'sendAndRedirect')]"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352