python-3.x - 如何使用 Python 在 xpath selenium 中选择多行值?
问题描述
这就是我想要抓取的表格的 html 代码的样子
这是我要抓取的页面的完整 html 代码,请检查这个:https://pastebin.com/uMhqJmrf
这是我的 Python 脚本。它仅用于抓取第一行,我想抓取表中显示的所有多行。这就是我想要抓取数据的表格的样子。https://prnt.sc/vnrx2n。
import urllib
import requests
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
try:
options = Options()
options.headless = True
driver = Chrome('C:/Users/poison/Downloads/chromedriver_win32/chromedriver.exe', options=options)
#driver = Chrome('C:/Users/poison/Downloads/chromedriver_win32/chromedriver.exe')
driver.get("https://redacted.com/root/Default.aspx")
username_form = driver.find_element_by_id("txtLoginID")
password_form = driver.find_element_by_id("txtPassword")
security_code_form = driver.find_element_by_id("txtCaptcha")
captcha_image = driver.find_element_by_id("Image1Captcha")
captcha_url = captcha_image.get_attribute("src")
captcha_code = captcha_url.split("?Str=")[1]
username_form.send_keys("redacted")
password_form.send_keys("redacted")
security_code_form.send_keys(captcha_code)
driver.find_element_by_id("btnLogin").click()
driver.get("https://redacted.com/root/Admin/Recharge_PendingRequest.aspx")
driver.find_element_by_xpath('//td[./select[@name="ctl00$ContentPlaceHolder1$ddlServiceType"]]').click()
driver.find_element_by_xpath('//div[@class="chosen-search"]/input').send_keys('dth\n').click()
phone = driver.find_element_by_xpath('//tr[@class="RowStyle"]/td[4]').text
amount = driver.find_element_by_xpath('//tr[@class="RowStyle"]/td[5]').text
operator = driver.find_element_by_xpath('//tr[@class="RowStyle"]/td[6]').text
# recharge_amount
ResultText = "ID number: " + phone + "\n" + "Amount: " + amount + "\n" + "Operator: " + operator
ParsedResultText = urllib.parse.quote_plus(ResultText)
requests.get("https://api.telegram.org/bot1405ddddddsQL60wRzOCaSEi9WB5twPoP5Euw/sendMessage?chat_id=-10dddddd2091""&text={}".format(ParsedResultText))
driver.quit()
except Exception:
print("No pending Recharge Request!!!")
解决方案
您只从一行中选择元素。
尝试这样的事情:
rows = driver.find_elements_by_xpath('//tr[@class="RowStyle"]') #get all the row elements
for row in rows: #loop through each row
#pluck out the elements from each row
phone = row.find_element_by_xpath('./td[4]').text
amount = row.find_element_by_xpath('./td[5]').text
operator = row.find_element_by_xpath('./td[6]').text
print(phone, amount, operator)
现在你实际上想要选择一种不同的方式来存储你选择的值,但这给了你一个要点,
推荐阅读
- jquery - jQuery:获取图像高度+如果需要添加填充
- react-native - 降低抽屉打开速度
- c# - 如何将自定义 ConfigurationSection 添加到程序集?
- typescript - 无法理解 Angular 如何将 json 映射到对象
- google-bigquery - bq load:“加载操作中的 BigQuery 错误:未找到:项目 ...”
- elasticsearch - Elasticsearch 查询排除另一个查询的结果
- python - Python matplotlib 操作以在绘图标题中使用它
- gradle - Gradle - 构建 Java Web 应用程序
- bash - bash脚本中的xset不起作用
- winforms - 需要在 Powershell 中创建、使用和存储动态对象名称