如何使用硒单击html中的文本?

时间:2019-07-15 06:57:47

标签: python selenium selenium-webdriver webdriver webdriverwait

我有这样的地区名称:阿南塔普尔(Anantapur),Chittoor等。我有13个地区。我必须单击每个区域。这是HTML的样子:

</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>

我已经尝试过Selenium来单击文本,但是以某种方式我无法做到这一点。

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)")

我需要单击文本Anantapur,Chittoor等。但这不起作用。仅供参考:这是网站:http://65.19.149.160/gws/reports.aspx

3 个答案:

答案 0 :(得分:0)

更多有关您被困在何处的信息将帮助我们以更好的方式回答问题。但是,所需元素是JavaScript启用的元素,因此对于元素上的click(),您需要为所需element_to_be_clickable()引入 WebDriverWait ,并且可以使用以下{{ 3}}:

  • 代码块:

    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")
    
  • 控制台输出:

    Data from Anantapur district
    Data from Chittoor district
    

答案 1 :(得分:0)

要单击每个District名称 首先,您需要获取网络表上可用的总分区数,然后iterate每个分区并单击。单击navigatesnew page链接后,您需要使用driver.back()返回初始页面并继续。

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()

控制台上的输出:

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

答案 2 :(得分:0)

  1. 请考虑使用更清晰的XPath定位器,因为这些//tr[i]//td[2]不可读且非常脆弱,如果进行最少的DOM更改,定位器将变得无效。我建议坚持使用部分id attribute,例如:

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

    查看以下材料以掌握XPath:

  2. 一旦您离开页面并返回,所有WebElements都将失效,您将不得不再次找到它们。为避免这种情况,您可以使用list comprehension并将WebElement列表转换为字符串列表(区名称)

    districts = [link.text for link in driver.find_elements_by_xpath("//a[contains(@id,'lnkbtnDist')]")]
    
  3. 假设以上所有内容,您都可以将代码修改为:

    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)")