简体   繁体   English

Select 元素使用 XPATH 和 Python?

[英]Select element using XPATH with Python?

I am trying to determine the number of pages of data generated by the Indian Central Pollution Controal Board.我正在尝试确定印度中央污染控制委员会生成的数据页数。 Here is an example of output .这是output 的示例 Following https://github.com/RachitKamdar/Python-Scraper , I used selenium/pythonhttps://github.com/RachitKamdar/Python-Scraper之后,我使用了 selenium/python

maxpage = int(browser.find_elements(By.XPATH,"//*[@id='DataTables_Table_0_paginate']/span/a")[-1].text)

but this produces an empty array.但这会产生一个空数组。 I am really not sure what I am doing wrong.我真的不确定我做错了什么。 Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks谢谢

You have to add expected condition to wait until the page loaded the data.您必须添加预期条件以等待页面加载数据。
You can wait for visibility of element you are using and after that get it's text, like this:您可以等待您正在使用的元素的可见性,然后获取它的文本,如下所示:

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

wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='DataTables_Table_0_paginate']/span/a")))
maxpage = int(browser.find_elements(By.XPATH,"//*[@id='DataTables_Table_0_paginate']/span/a")[-1].text)

You might want to try getattribute('textContent')您可能想尝试 getattribute('textContent')

In your case:在你的情况下:

maxpage=browser.find_element_by_xpath("(//*[@id='DataTables_Table_0_paginate']/span/a)[last()]").getattribute('textContent')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM