【问题标题】:extracting corresponding values in the column of dynamic webtable based on the given xpath using selenium and python使用selenium和python根据给定的xpath提取动态webtable列中的对应值
【发布时间】:2019-10-11 04:58:17
【问题描述】:

我有以下动态 webtable

(checkbox)   ID_No  country_code  Date          Time                  FileName
            1A       5J           10/04/2019    12:05:45           filename_12:05
            1A       5J           10/04/2019    13:05:45           filename2
            3A       8J           10/03/2019    14:05:45           filename2
            4A       9J           10/04/2019    14:08:45           filename1

id_no 的左侧有一个复选框。

我有以下数据框

ID_No  country_code  Date            Time               FileName
1A       5J           10/04/2019    
1A       5J           10/04/2019    

我只想检查与匹配 country_code,ID_No 对应的动态 webtable 中的行 和数据框中的日期,并在数据框中填充时间和文件名值

我在一次选择 1 个复选框的循环中编写下面的代码

for index,row in df.iterrows():
        id_no=row['ID_No']
        country_code=row['country_code']
        date=row['Date']
        driver = webdriver.Chrome() 
        driver.get(website_URL) 
        driver.find_element_by_xpath("//tr[td[contains(text(),date)] and 
        td[contains(text(),country_code)] and td[contains(text(),id_no)] ]//input").click()

在这种情况下,country_code='5J' and ID_No='1A' and Date='10/04/2019' 当我在 chrome 控制台中使用这个 xpath 时:

$x("//tr[td[contains(text(),'10/04/2019')] and 
td[contains(text(),'5J')] and td[contains(text(),'1A')] ]")[0]

这包含满足上述条件的第一行。我得到了此 webtable 行的以下 HTML

<tr class="class1">
  <td width="2%"> </td>   <!–– checkbox ––>
  <td width="2%"> 1A </td>
  <td width="2%"> 5J </td>
  <td width="2%"> 10/04/2019 </td>
  <td width="2%"> 12:05:45 </td>
  <td width="2%"> filename_12:05 </td>
</tr>

如何从我选择的复选框对应的动态webtable中提取相应的文件名和时间?

在上述情况下,在第一次迭代中,extracted_filename='filename_12:05' and time='12:05:45' second_iteration,extracted_filename='filename2' and time='13:05:45

【问题讨论】:

  • 你可以试试xpath轴。 //tr[td[contains(text(),'10/04/2019')] 和 td[contains(text(),'5J')] 和 td[contains(text(),'1A')] ] //td[5] //tr[td[contains(text(),date)] and td[contains(text(),country_code)] and td[contains(text(),id_no)] ]//td[ 5] : 时间 //tr[td[contains(text(),date)] and td[contains(text(),country_code)] and td[contains(text(),id_no)] ]//td[6] : 文件名

标签: python selenium xpath


【解决方案1】:

你能试试这些 xpath 并告诉我进展如何。

这会给你匹配行的时间

time= driver.find_element_by_xpath("//tr[td[contains(text(),date)] and td[contains(text(),country_code)] and td[contains(text(),id_no)] ]//input/../following-sibling::td[4]").text

这将为您提供匹配行的文件名

filename=driver.find_element_by_xpath("//tr[td[contains(text(),date)] and td[contains(text(),country_code)] and td[contains(text(),id_no)]]//input/../following-sibling::td[5]").text

使用您的代码:

for index,row in df.iterrows():
            id_no=row['ID_No']
            country_code=row['country_code']
            date=row['Date']
            driver = webdriver.Chrome() 
            driver.get(website_URL) 
            driver.find_element_by_xpath("//tr[td[contains(text(),date)] and td[contains(text(),country_code)] and td[contains(text(),id_no)] ]//input").click()
            time= driver.find_element_by_xpath("//tr[td[contains(text(),date)] and td[contains(text(),country_code)] and td[contains(text(),id_no)] ]//input/../following-sibling::td[4]").text
            filename=driver.find_element_by_xpath("//tr[td[contains(text(),date)] and td[contains(text(),country_code)] and td[contains(text(),id_no)]]//input/../following-sibling::td[5]").text

【讨论】:

    【解决方案2】:

    处理动态元素 Induce WebDriverWait 并遵循 xpath。

    返回时间

    print(WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//tr[td[contains(text(),'" + date + "')] and  td[contains(text(),'" + country_code +"')] and td[contains(text(),'" + id_no + "')] ]//input/following::td[4]"))).text)
    

    返回文件名

    print(WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//tr[td[contains(text(),'" + date + "')] and  td[contains(text(),'" + country_code +"')] and td[contains(text(),'" + id_no + "')] ]//input/following::td[5]"))).text)
    

    要执行上述代码,您需要导入以下代码。

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

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 2021-06-18
      • 1970-01-01
      • 2020-08-25
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      相关资源
      最近更新 更多