Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
find element...click() dont work
#1
Hello world,
I have the next function

  
f1="/html/body/div[2]/main/div....section[1]"
f2="/html/body/div[2]/main/div....section[2]"
f3="/html/body/div[2]/main/div....section[3]"...

f=[f1,f2,f3....]

b=0

try:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    d1=driver.find_element_by_xpath("/html/body/div[2]/..section[1]")
    b=b+1

    d2=driver.find_element_by_xpath("/html/body/div[2]/...section[2]")
    b=b+1

    d3=driver.find_element_by_xpath("/html/body/div[2]/...section[3]")
    b=b+1....

 
except NoSuchElementException as e:
	print("no more")

def my_function(i,driver):
	while i<b:
		driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
		import time
		time.sleep(2)
		element=driver.wait.until(EC.presence_of_element_located((By.XPATH,f[i])))
		driver.find_element_by_xpath(f[i]).click()
		try:
			import time
			time.sleep(1)
			print(" click again")
			driver.find_element_by_xpath(f[i])
			driver.find_element_by_xpath(f[i]).click()
		except NoSuchElementException as e:
			print("no need double click ")

		try: 
	    
			myDynamicElement = driver.find_element_by_xpath("/html/section[8]/...input")                                                  
		
			driver.find_element_by_xpath("/html/section[8]/...input").click()           
		
		except NoSuchElementException as e:
			driver.wait.until(EC.presence_of_element_located((By.XPATH,"/html/section[9]/..input")))
			
			driver.find_element_by_xpath("/html/section[9]/...input").click()
		import time
		time.sleep(2)
		driver.back()
		i=i+1



if b>0:
	my_function(0,driver) 
and it runs perfect, the problem inside the function I need to have the second
print(" click again")
driver.find_element_by_xpath(f[i])
driver.find_element_by_xpath(f[i]).click()

because sometimes the python find and click, but the page is not loading, so
i create this second step for make sure the button is clicked, and works, but sometimes this is not working and looks I have to create another same code find....click()
Do you know why it happens, have one solution.

thank you world
Reply
#2
maybe I was not clear what was the problem, or don't have a solution
Reply
#3
Quote:maybe I was not clear what was the problem, or don't have a solution
It wasn't clear to me what you are trying to do.
Reply
#4
Ok I will try in a different way.
Basically I´m doing this:

In page 1 I have x items.
I select the first, and I go to the page 2.
In page 2 I have a button ( can be in two position, 2 different Xpath )
I select the button and go back (page 1)
...
and I repeat the cycle, until i select all the item in page 1



The problem is when I´m in page 1 I have to do:

element=driver.wait.until(EC.presence_of_element_located((By.XPATH,f[i])))
driver.find_element_by_xpath(f[i]).click()
try:
import time
time.sleep(1)
print(" click again, because the previous click dind´t work ")
driver.find_element_by_xpath(f[i])
driver.find_element_by_xpath(f[i]).click()


In bold is the same button for the same page. and I don't know why I don't go to page 2 if the code press click() in the first step.
Why he have to pass in try step and click() again in the same button in the same page.

Because what I´m seeing I need 100x
driver.find_element_by_xpath(f[i]).click()
driver.find_element_by_xpath(f[i]).click()
driver.find_element_by_xpath(f[i]).click()
driver.find_element_by_xpath(f[i]).click()
.....

for make sure I go to page 2
Reply
#5
Try clicking using javascript

element = driver.find_element_by_xpath(f[i])
driver.execute_script("arguments[0].click();", element)

If this fails, post the error message
Reply
#6
Hello law and thank you for your help, I use your code and works the same, in the 3rd or 4th cycle, the python do the find_element...click() and don't go the second page.

I did different experiments, and I realize, the problem is I don't give time enough for the webpage (when go backs) for loading, because if I set a time in the beginning in the function I can run perfectly
like this:
def my_function(i,driver):
    while i<b:
        Import time
        time.sleep(8)
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        import time
        time.sleep(8)....
my question is why if the python find the Xpath in my code element=driver.wait.until(EC.presence_of_element_located((By.XPATH,f[i])))

why he can not click, have a different way for I do not need to use the import time...
Reply
#7
I get you know, actually the problem is not python,on the website you are scrapping some elements are loading earlier than others.By the time a click is happening the button has not fully loaded.
2 ways to solve this

(i)import time only once at the beginning of your program and use time.sleep(sec)
import time
def my_function(i,driver):
    while i<b:
        
        time.sleep(8)
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        
        time.sleep(8)....
(ii)element=WebDriverWait(driver, 10).until(ec.visibility_of_element_located((By.XPATH, f[i])))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to find element in a deeply nested html structure Orientation_group 5 1,297 Oct-09-2023, 10:13 AM
Last Post: Larz60+
  Selenium suddenly fails to find element Pavel_47 3 6,318 Sep-04-2022, 11:06 AM
Last Post: Pavel_47
  Seleniun does not work with a website element flote21 2 2,380 Jan-12-2021, 07:38 PM
Last Post: flote21
  Unable to click element in web page Kalpana 0 1,871 Jun-25-2020, 05:20 AM
Last Post: Kalpana
  Find element using xpath engli 3 3,950 May-20-2020, 08:56 AM
Last Post: Larz60+
  Find Dynamic Input Element Class Problem wazacko 0 1,606 Apr-03-2020, 11:46 AM
Last Post: wazacko
  bypassing find element click() windows11 1 1,885 Apr-02-2020, 11:55 AM
Last Post: Larz60+
  How find link element zinho 3 3,126 Mar-31-2020, 12:29 PM
Last Post: snippsat
  wait for element and click windows11 2 2,658 Mar-21-2020, 09:23 PM
Last Post: windows11
  How to find which frame/iframe my element belongs to? smaria 1 2,062 Nov-18-2019, 09:21 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020