【问题标题】:How to iterate over table and get open the tab that satisfies a condition如何遍历表并打开满足条件的选项卡
【发布时间】:2020-09-14 16:22:08
【问题描述】:

我需要遍历一个表来获取“GST Invoice No.”的值。并仅打开第三位为数字 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 的那个?

【问题讨论】:

  • 您好,问题出在哪里?
  • 您好,对于不同的 PNRS,发票是分散的,没有按特定顺序排列。我想打开那些 GST Invoice No 的第三位数字为 2 的发票。如何循环并获取这些发票?
  • 您可以使用带有 ul[class='gst-invoice-list list-inline']/li 的 xpath 而不是正则表达式 /text()='' 来匹配。
  • 这会检查所有的消费税号码吗?
  • 您需要在此之后使用 /text()='' 并检查第 3 位中的数字 2。

标签: python html selenium selenium-webdriver


【解决方案1】:

包含 GST 发票编号的表格需要一些时间来加载“GST 发票编号”。在读取数据进行比较之前应用一些等待。检查下面的代码,提取发票编号并比较第 3 位数字,然后单击“查看发票”链接以查看发票”

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()

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'));
        #break

driver.quit()

【讨论】:

  • 嗨 Dilip,上面的代码给出了 ElementClickInterceptedException: element click intercepted: 我尝试使用 ele.find_element(By.XPATH, 'li[2]/a[1]').click() 仍然得到同样的错误
  • @ShwetaSave - 此代码搜索第一个匹配项并单击 ViewInvoice,它将在新选项卡中打开,执行将退出 for 循环。如果您想ViewInvoice 处理所有事件,则需要使用 getWindowHandles 来实现相同的目的。 ElementClickInterceptedException: element click intercepted: 您一定会遇到错误,因为当您检查与该页脚图像重叠的其他 ViewInvoice 按钮时,该页面上的图像将获得点击。
  • 它第一次遇到时没有打开。
  • 你删除了break 声明?
  • 是的,但后来我确实添加了它。
【解决方案2】:

没有正则表达式,目前只能通过 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)

显示以下内容

MH2192004AA58892
MP2192004AA05359

【讨论】:

  • 只开这两张发票我现在应该使用 url 代替驱动程序,对吧?
  • 您几乎可以查看 dilips 的答案并弄清楚他做了什么。除了使用休息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-19
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2016-03-24
相关资源
最近更新 更多