Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要单击与给定文本匹配的下载按钮_Python_Selenium_Selenium Webdriver_Automation - Fatal编程技术网

Python 需要单击与给定文本匹配的下载按钮

Python 需要单击与给定文本匹配的下载按钮,python,selenium,selenium-webdriver,automation,Python,Selenium,Selenium Webdriver,Automation,我试图下载在表中写入了税务发票的发票,并对表元素进行迭代,获取所需的详细信息。但当我点击下载按钮时,它会下载同一张发票两次。如何下载第二张“税务发票”?下图: 以及html代码: 代码如下: import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from sele

我试图下载在表中写入了税务发票的发票,并对表元素进行迭代,获取所需的详细信息。但当我点击下载按钮时,它会下载同一张发票两次。如何下载第二张“税务发票”?下图:

以及html代码:

代码如下:

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




driver = webdriver.Chrome(executable_path=r'D:/Chrome driver/chromedriver.exe')
driver.get("the link")

time.sleep(10)

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="GST"]/div/div/a[2]/span[1]'))).click()

driver.switch_to.frame('ifgstdownloadnFrame')
driver.find_element_by_xpath('//*[@id="txtpnr"]').send_keys('QYJ27J')

driver.find_element_by_xpath('//*[@id="btnSubmit"]').click()

mytable = driver.find_element_by_css_selector("table.gstInvoiceGrid")

for row in mytable.find_elements_by_css_selector('tr'):
    for cell in row.find_elements_by_tag_name('td'):
        if 'Tax Invoice' in cell.text:
            print()
            driver.find_element_by_css_selector('input.download.downloadbutton').click()
time.sleep(10)
            
#driver.quit()

您需要使用
dot
更改逻辑以查找
中间
单元格,使用以下任一代码逻辑,该逻辑将单击具有
税务发票
的每个下载按钮

for row in mytable.find_elements_by_css_selector('tr'):
    for cell in row.find_elements_by_xpath('./td'):
        if 'Tax Invoice' in cell.text:
            print()
            row.find_element_by_xpath(".//input[@value='Download']").click() 


您需要使用
dot
更改逻辑以查找
中间
单元格,使用以下任一代码逻辑,该逻辑将单击具有
税务发票
的每个下载按钮

for row in mytable.find_elements_by_css_selector('tr'):
    for cell in row.find_elements_by_xpath('./td'):
        if 'Tax Invoice' in cell.text:
            print()
            row.find_element_by_xpath(".//input[@value='Download']").click() 


此行:driver.find_element_by_css_selector('input.download.downloadbutton')。click()与您所在的行无关。我会尝试一个以id或发票为目标的选择器。这使得代码更加简单。无需查找表格或行/单元格。表格会不断更改,有时会有四个条目,有时会有六个甚至两个条目,因此我正在匹配文本,由于表格中的条目不会固定,id将不断更改。//td[text()='Tax Invoice']/../td:last child/input应该找到所有输入标记,然后单击感谢您的回答。我使用的是驱动程序而不是行,这就是它下载第一张税务发票的原因。此行:driver.find\u element\u by\u css\u选择器('input.download.downloadbutton')。click()与您所在的行无关。我会尝试一个以id或发票为目标的选择器。这使得代码更加简单。无需查找表格或行/单元格。表格会不断更改,有时会有四个条目,有时会有六个甚至两个条目,因此我正在匹配文本,由于表格中的条目不会固定,id将不断更改。//td[text()='Tax Invoice']/../td:last child/input应该找到所有输入标记,然后单击感谢您的回答。我使用的是驱动程序而不是行,这就是为什么它下载了第一张税务发票。谢谢你的回答。“row.find_element_by_xpath(“.//input[@value='Download']”)可以很好地使用。单击()”。谢谢您的回答。可以通过xpath(“.//input[@value='Download']”)使用“row.find_element”。单击()。