Python Forum

Full Version: find element...click() dont work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
maybe I was not clear what was the problem, or don't have a solution
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.
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
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
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...
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])))