通过 xpath 查找多个 td 标签 [英] Find by xpath for multiple td tags

查看:79
本文介绍了通过 xpath 查找多个 td 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这个网站下载 PDF 文件 https://www.asx.com.au/asx/statistics/prevBusDayAnns.do 如果满足两个条件.第一个条件是ASX 代码"必须与列表中的一个代码匹配.第二个条件是标题"必须与实质性持有的变化"相匹配.如果ASX 代码"=SPL",我当前的代码仅通过 xpath 查找.

I would like to download PDF files from this website https://www.asx.com.au/asx/statistics/prevBusDayAnns.do if two conditions are met. The first condition is that the 'ASX Code' has to match one of the codes in a list. The second condition is that the 'Headline' has to match 'Change in substantial holding'. My current code only finds by xpath if the 'ASX Code' = 'SPL'.

我想要实现的目标的示例:

data1 = ['SPL', 'WBC', 'AAA']
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//table//tr//td[text()={data1}]/following-sibling::td[3]/a"))).click()

我的代码:

chromeOptions=webdriver.ChromeOptions()
prefs = {"plugins.always_open_pdf_externally": True}
chromeOptions.add_experimental_option("prefs",prefs)
driver=webdriver.Chrome(executable_path=r"C:\Users\Harrison Pollock\Downloads\Python\chromedriver_win32\chromedriver.exe",chrome_options=chromeOptions)
driver.get("https://www.asx.com.au/asx/statistics/prevBusDayAnns.do")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//table//tr//td[text()='SPL']/following-sibling::td[3]/a"))).click()
WebDriverWait(driver,15).until(EC.number_of_windows_to_be(2))
driver.switch_to.window(driver.window_handles[-1])
WebDriverWait(driver,15).until(EC.element_to_be_clickable((By.XPATH,"//input[@value='Agree and proceed']"))).click()

推荐答案

我找不到带有 ASX 代码的数据集 data1 = ['SPL', 'WBC', 'AAA']在我所在位置的网页上.但是这里是如何按顺序下载多个 ASX 代码的示例.

I couldn't find the data set with ASX code data1 = ['SPL', 'WBC', 'AAA'] on web page at my location.However here is the example how to down load multiple ASX code in a sequence.

数据集:data1 = ['SW1', 'AME', 'BGA','PPT','AMP']

href 值存储在列表中,然后迭代列表并单击同意"按钮下载 pdf.

Store href value of the in a list and then iterate the list and click on Agreed button to download the pdf.

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

chromeOptions=webdriver.ChromeOptions()
prefs = {"plugins.always_open_pdf_externally": True}
chromeOptions.add_experimental_option("prefs",prefs)
driver=webdriver.Chrome(executable_path=r"C:\Users\Harrison Pollock\Downloads\Python\chromedriver_win32\chromedriver.exe",chrome_options=chromeOptions)
driver.get("https://www.asx.com.au/asx/statistics/prevBusDayAnns.do")
data1 = ['SW1', 'AME', 'BGA','PPT','AMP']

pdfUrls=[]
for d in data1:
    try:
       pdfurl=driver.find_element_by_xpath("//table//tr//td[text()='{}']/following-sibling::td[3]/a[contains(.,'{}')]".format(d,"Change in substantial holding")).get_attribute("href")
       pdfUrls.append(pdfurl)
    except:
        print("No ASX code found with Headline Change in substantial holding : " + d)


for pdfurl in pdfUrls:
    driver.get(pdfurl)
    WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Agree and proceed']"))).click()
    time.sleep(10)  # pause to check download
    print("Downloaded pdf file")

这篇关于通过 xpath 查找多个 td 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆