【问题标题】:How to select multiple rows values in xpath selenium using Python?如何使用 Python 在 xpath selenium 中选择多行值?
【发布时间】:2020-11-22 12:05:24
【问题描述】:

这就是我要抓取的表格的 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!!!")

【问题讨论】:

  • 图片和HTML代码好像不匹配。您的图像中有多行,而 HTML 代码中只有 1 行。一般来说,我会首先尝试查找所有行,然后遍历它们并在其中找到您的 <td> 标签。
  • 这是我要废弃的页面的完整 html 代码,请查看:pastebin.com/uMhqJmrf

标签: python-3.x selenium xpath web-scraping


【解决方案1】:

您只是从一行中选择元素。

试试这样的:

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)

现在您实际上想要选择一种不同的方式来存储您选择的值,但这为您提供了 jist,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-13
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多