0

I'm trying to use python and selenium to click an element without sucess the same way I do when I want to click button elements.

python --version
Python 2.7.16

print selenium.__version__
3.141.0

chromedriver --version
ChromeDriver 2.36

chromium-browser --version
Chromium 65.0.3325.181

This is the tag:

<a data-fblog="the_button" href="javascript:" id="the_btn" class="the_btn" title="Goto">Goto</a>

(that clearly is not defined as a button)

and this is the python part responsible for the click in that tag:

chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
driver = webdriver.Chrome('PATHTOCRHROMEDRIVER/chromedriver',options=chrome_options)
driver.get('https://www.urltogetinfo.com')

try:
    the_button = driver.find_element_by_id('the_btn')
    the_button.click()
    #Also tried these aproaches without sucess:
    #the_button = driver.find_element_by_id('the_btn').send_keys(Keys.RETURN)
    #the_button = driver.find_element_by_id('the_btn').send_keys(Keys.ENTER)
    sleep(5)
except:
    print("Problem clicking the button")
    #would like to log the exception here
    pass

What could I be doing wrong?

Eunito
  • 416
  • 5
  • 22

2 Answers2

1

Try the below options

  1. Try adding implicit wait after webdriver initialization or before clicking the element. driver.implicitly_wait(15)
  2. Try adding explicit wait for the button

WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "the_btn")))

  1. Check whether the button is present inside the iframe or not.if it present inside iframe tag , switch to iframe before clicking on the button
Yosuva Arulanthu
  • 1,444
  • 4
  • 18
  • 32
  • driver.implicitly_wait(15) worked fine! Need to google and understand how this solved and how the other solutions would too. Thank you. – Eunito Oct 08 '19 at 17:47
  • It's not great practice to mix `driver.implicitly_wait(15)` and also invoke `WebDriverWait`. This is an example of mixing implicit and explicit waits, which is not advised in automation, as you can run into unexpected wait times. Using one or the other is fine, but both together is not advisable. https://stackoverflow.com/questions/29474296/clarification-of-the-cause-of-mixing-implicit-and-explicit-waits-of-selenium-doc – CEH Oct 08 '19 at 17:58
1

You could try invoking a WebDriverWait on the element's existence, then clicking with Javascript here.

the_button = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.ID, "the_btn")))
driver.execute_script("arguments[0].click();", the_button)
CEH
  • 5,701
  • 2
  • 16
  • 40