首页 > 解决方案 > 如何使用 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!!!")

标签: python-3.xseleniumxpathweb-scraping

解决方案


您只从一行中选择元素。

尝试这样的事情:

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)

现在你实际上想要选择一种不同的方式来存储你选择的值,但这给了你一个要点,


推荐阅读