chidimo
chidimo

Reputation: 2948

How to click a javascript javascript:__doPostBack('ac$w$PC$PC$grid','Page$2')

I'm scraping a page using selenium and python. The data is paginated and the table data looks like below.

<td>
    <a href="javascript:__doPostBack('ac$w$PC$PC$grid','Page$2')">2</a>
</td>

The challenge now is to get selenium to click on this link and advance to the next page.

There's a SO question that tries to address the problem but its not in python.

There are also a lot of SO questions that try to address this using execute_script but none of them addresses the added complication of a javascript function with seemingly two arguments.

How do I get selenium to click on this link and advance to the next page? Any help or pointer is highly appreciated.

Upvotes: 1

Views: 1570

Answers (2)

GabrielaGU
GabrielaGU

Reputation: 11

This should work:

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

[...]

WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH,"//td/a[text()='2']")))
driver.find_element_by_xpath("//td/a[text()='2']").click()

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193108

You need to induce WebDriverWait for the staleness of the element and can use the following solution:

WebDriverWait(driver, 10).until(EC.staleness_of(driver.find_element_by_xpath("//td/a[text()='2']")))
driver.find_element_by_xpath("//td/a[text()='2']").click()

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

You will find a relevant discussion in How do I wait for a JavaScript __doPostBack call through Selenium and WebDriver

Upvotes: 1

Related Questions