0

I have got district names like these : Anantapur, Chittoor etc.. I have 13 districts. I have to click on each district. This is how HTML looks like:

</tr>
<tr class="gridRowStyle" align="center" style="color:#000066;">
    <td>
                                        1
    </td>
    <td align="left">
        <a id="ctl00_ContentPlaceHolder1_Grid_Dist_ctl02_lnkbtnDist" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Grid_Dist$ctl02$lnkbtnDist','')">Anantapur</a>
    </td>
    <td align="right">
        <span id="ctl00_ContentPlaceHolder1_Grid_Dist_ctl02_lblDman">63</span>
    </td>
    <td align="right">
        <span id="ctl00_ContentPlaceHolder1_Grid_Dist_ctl02_lblMvill">2856</span>
    </td>
    <td align="right">
        <span id="ctl00_ContentPlaceHolder1_Grid_Dist_ctl02_lblMcon">215441</span>
    </td>
    <td align="right">
        <span id="ctl00_ContentPlaceHolder1_Grid_Dist_ctl02_lblDNExist">49681</span>
    </td>
    <td align="right">
        <span id="ctl00_ContentPlaceHolder1_Grid_Dist_ctl02_lblDExist">165760</span>
    </td>
    <td align="right">
        <span id="ctl00_ContentPlaceHolder1_Grid_Dist_ctl02_lblMupcon">215437</span>
    </td>
    <td align="right">
        <span id="ctl00_ContentPlaceHolder1_Grid_Dist_ctl02_lblPer">100.00</span>
    </td>
</tr>
<tr class="gridAlterRowStyle" align="center" style="color:#000066;">
    <td>
                                        2
    </td>
    <td align="left">
        <a id="ctl00_ContentPlaceHolder1_Grid_Dist_ctl03_lnkbtnDist" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Grid_Dist$ctl03$lnkbtnDist','')">Chittoor</a>

I have tried Selenium to click on the text but somehow I am not able to do that.

Python

a=[2,14]
for i in a:
driver.find_element_by_xpath("//table[@id='ctl00_ContentPlaceHolder1_Grid_Dist']//tr[i]//td[2]").click()
driver.execute_script("window.history.go(-1)")

I need to click on the text Anantapur, Chittoor etc.. But it is not working. FYI: This is the website: http://65.19.149.160/gws/reports.aspx

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
G.S. J
  • 233
  • 1
  • 8

3 Answers3

0

A bit of more information about where exactly you are stuck would have helped us to answer the question in a better way. However the desired elements are JavaScript enabled element so to click() on the element you need to induce WebDriverWait for the desired element_to_be_clickable() and you can use the following Locator Strategies:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    chrome_options = webdriver.ChromeOptions() 
    chrome_options.add_argument("start-maximized")
    # chrome_options.add_argument('disable-infobars')
    driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    base_url = "http://65.19.149.160/gws/reports.aspx"
    driver.get(base_url)
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Anantapur"))).click()
    print("Data from Anantapur district")
    driver.get(base_url)
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Chittoor"))).click()
    print("Data from Chittoor district")
    
  • Console Output:

    Data from Anantapur district
    Data from Chittoor district
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

To click on Each District name First you need to get the count of the total district available on the web table and then iterate each district and click.Once click on link it navigates to a new page so you need to use driver.back() to return to initial page and go on.

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

driver=webdriver.Chrome()
driver.get("http://65.19.149.160/gws/reports.aspx")

Districts=WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.XPATH,"//table[@id='ctl00_ContentPlaceHolder1_Grid_Dist']//a[contains(@id,'ctl00_ContentPlaceHolder1_Grid_Dist_ctl')]")))

for d in range(len(Districts)):
  Districts = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH,"//table[@id='ctl00_ContentPlaceHolder1_Grid_Dist']//a[contains(@id,'ctl00_ContentPlaceHolder1_Grid_Dist_ctl')]")))
  print("District Name clicked :" + Districts[d].text)
  Districts[d].click()
  driver.back()

Output On Console:

District Name clicked :Anantapur
District Name clicked :Chittoor
District Name clicked :East Godavari
District Name clicked :Guntur
District Name clicked :Kadapa
District Name clicked :Krishna
District Name clicked :Kurnool
District Name clicked :Nellore
District Name clicked :Prakasam
District Name clicked :Srikakulam
District Name clicked :Visakhapatnam
District Name clicked :Vizianagaram
District Name clicked :West Godavari
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • Thanks you! Though I have used Dmitri's solution, I want to mention that your solution is also working perfectly. – G.S. J Jul 15 '19 at 12:33
0
  1. Consider using a more clear XPath locator as these //tr[i]//td[2] are not readable and very fragile, in case of minimal DOM change the locators will become invalid. I would recommend sticking to partial id attribute like:

    //a[contains(@id,'lnkbtnDist')]
    

    Check out the following material to master XPath:

  2. Once you navigate away from the page and come back all WebElements will be invalidated and you will have to find them once again. To avoid this you can use list comprehension and convert the list of WebElements to the list of strings (district names)

    districts = [link.text for link in driver.find_elements_by_xpath("//a[contains(@id,'lnkbtnDist')]")]
    
  3. Assuming all above you can amend your code to look like:

    driver.get("http://65.19.149.160/gws/reports.aspx")
    
    districts = [link.text for link in driver.find_elements_by_xpath("//a[contains(@id,'lnkbtnDist')]")]
    
    for district in districts:
        driver.find_element_by_link_text(district).click()
        driver.execute_script("window.history.go(-1)")
    
Dmitri T
  • 159,985
  • 5
  • 83
  • 133