如何迭代表并打开满足条件的选项卡

我需要迭代一个表来获取“GST 发票号”的值。并打开仅包含第三位为数字 2 的发票号的“查看发票”表。


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') # Get local session(use webdriver.Chrome() for chrome) 


driver.get("URL")




WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//input[@id='PNRId']"))).send_keys("SHFYGW")

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='GstRetrievePageInteraction']"))).click()



#print(toJourney.find_element_by_xpath('/tbody/tr/td[2]/ul'))


如何继续迭代表?我尝试了以下方法,但它只提供了一个值


uls = driver.find_elements(By.CLASS_NAME, "gst-invoice-list.list-inline")

如何迭代只打开第三位数字为 2 的数字?


函数式编程
浏览 118回答 2
2回答

catspeake

该表包含 GST 发票编号,需要一些时间来加载“GST 发票编号”。在读取数据进行比较之前等待一段时间。检查下面的代码,提取发票编号并比较第三位数字,然后单击“查看发票”链接查看发票from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Chrome()driver.get("URL")WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='PNRId']"))).send_keys("SHFYGW")WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='GstRetrievePageInteraction']"))).click()WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "gst-invoice-list.list-inline")))elements = driver.find_elements(By.CLASS_NAME, "gst-invoice-list.list-inline")for ele in elements:    eleText = ele.find_element_by_tag_name("li").text    if eleText[2]=='2':        print(eleText)        #ele.find_element(By.ID, 'ViewInvoice').click()        driver.execute_script("arguments[0].click()", ele.find_element(By.ID, 'ViewInvoice'));        #breakdriver.quit()

慕斯王

目前没有正则表达式,只能通过 xpath 查找。driver.implicitly_wait(5)uls = driver.find_elements_by_xpath("//ul[@class='gst-invoice-list list-inline']//li")for url in uls:    if url.text[2]=='2': #^..2 would be a simple regex for digit 2 on 3rd index         print (url.text)显示以下内容MH2192004AA58892MP2192004AA05359
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python