-1

I am automating a website with Selenium, however click() is not working. It returns an element that can not be scrolled into view.

I used Javascript Executor to invoke click(), the code is written to find an element on the page clicked on after click(). However, Selenium attempts to find the element before it clicks on the website.

I checked the website while the script was running, and it attempts to find the element and then clicks on the page it's on. How do I fix this?

(code below)

I have tried waiting for the element to be clickable using WebDriver. However, it times out every time, so I am out of ideas. Any help is appreciated.

  Find_Section = Driver.find_element_by_xpath('//div[contains(text()="Text"]')
  Driver.execute_script("arguments[0].click();", Find_Section)
  Tester = Driver.find_element_by_partial_link_text('Test19')
  Tester.click()

Edit:

I believe i have figured out what the problem is according to WebDriver click() vs JavaScript click() click() behaves like a actual user does and execute_script does not and just calls the event so what i believe is happening is the execute_script is clicking on the event but not doing the same as click() so it isn't loading the page correctly.

The reason why i am using execute_script is because the click() method is throwing can not scroll element into view each time.

I may be wrong about the reason why i think it is not working if so correct me

Michael
  • 1
  • 1

1 Answers1

0

I finally figured it out since no one here would help me for anyone interested you need to insert a wait statement before the next element for example

try:
    time.sleep(2) # Could be WebDriverWait
    Find_Example = driver.find_element_by_partial_link_text('Example')
    time.sleep(2) # Try to make bot less noticeable 
    Find_Example.click()
except InvalidElement: # Defined in the import section
   # Syntax may be wrong i wrote this by hand in this little text window
   print("\033[31m Could Not Find The Desired Element [STATUS] \033[31m")
   exit()

Michael
  • 1
  • 1