Selenium click GDPR click button

sumixam :

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://allrecipes.co.uk/")
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

DebanjanB :

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
    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=279370&siteId=1