0

I'm a noob and trying to automate some online form filling in a certain site. My problem is that some buttons need some time before clicking them, otherwise they don't work (but no error!, execution continues).
My only solution so far is to add a time.sleep(6) before these buttons but this is not ideal.
I am trying to find a better solution.

So far, I have this function:

def Send_Click_dk(bywhat,what):
    WebDriverWait(browser, 10).until(EC.presence_of_element_located((bywhat,what)))
    WebDriverWait(browser, 10).until(EC.visibility_of(browser.find_element(bywhat, what)))
    WebDriverWait(browser, 10).until(EC.element_to_be_clickable(browser.find_element(bywhat, what)))
    browser.find_element(bywhat, what).click()

Send_Click_dk(By.NAME, "mainpanel_parentSection_1b0a0b")

First of all, is this a good approach? Am I misunderstanding something? Secondly, if this is the right approach, what else could I check before clicking the button? So far, all of these checks pass instantly and the .click() is executed but doesn't produce the expected result. Only by adding time.sleep(6) the clicking works as intended.

This is a snapshot of that particular part of the page, I'm having trouble copying the raw text. Pic of Html

The first marked button reveals the second marked button. But if without the time.sleep(x) it just stays closed without revealing the second one. Many thanks in advance for any help! Cheers!

Cos Dim
  • 101
  • 4

1 Answers1

1

You definitely can reduce all your code to a single line of

def Send_Click_dk(bywhat,what):    
    WebDriverWait(browser, 10).until(EC.element_to_be_clickable(browser.find_element(bywhat, what))).click()

Send_Click_dk(By.NAME, "mainpanel_parentSection_1b0a0b")

visibility_of expected condition includes presence_of_element_located since element can't be visible without being present.
element_to_be_clickable internally includes visibility_of.
Also WebDriverWait(browser, 10).until(EC.element_to_be_clickable(browser.find_element(bywhat, what))) returns web element object, so you can click it directly.
Also, make sure the mainpanel_parentSection_1b0a0b name attribute is a fixed value. 1b0a0b suffix seems to be dynamically generated

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Thank you for that! Very useful info but main issue was that some buttons dont get clicked even though the .click() method executed successfully. Any idea on that? – Cos Dim Nov 10 '22 at 14:52
  • This can be caused by several issues, for example the element you trying to click is not actually clickable. i.e. you can click it but this will do nothing. We need to see all your code including the page you working on to give more specific answer – Prophet Nov 10 '22 at 15:04
  • Added a picture of the page if it helps. Are pictures like this allowed? – Cos Dim Nov 10 '22 at 15:36
  • Please share the XML / HTML as a text – Prophet Nov 10 '22 at 15:41