Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.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 在selenium中迭代webelements总是只返回表的第一行_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python 在selenium中迭代webelements总是只返回表的第一行

Python 在selenium中迭代webelements总是只返回表的第一行,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,这是我的代码,首先我在all_trows变量中获取一个表的所有行,在对它进行迭代时,我期望得到不同的输出,因为每行中有不同的文本,但我总是获取表的第一个tr的值 url = 'https://www.un.org/Depts/ptd/eoi?field_commodity_group_eoi_rfi_tid=All&items_per_page=5' driver.get(url) driver.implicitly_wait(5) WebDriverWait(driver,5).un

这是我的代码,首先我在all_trows变量中获取一个表的所有行,在对它进行迭代时,我期望得到不同的输出,因为每行中有不同的文本,但我总是获取表的第一个tr的值

url = 'https://www.un.org/Depts/ptd/eoi?field_commodity_group_eoi_rfi_tid=All&items_per_page=5'

driver.get(url)
driver.implicitly_wait(5)
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.XPATH,"//*[@id='block-system-main']/div/div[contains(@class,'view-content')] /table/tbody")))
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.XPATH,"//*[@id='block-system-main']/div/div[contains(@class,'view-footer')]/div[contains(@class,'page_result')]")))

page = 0
while True:
    time.sleep(5)
    driver.implicitly_wait(10)
    current_page_flag = driver.find_element(By.XPATH , "//*[@id='block-system-main']/div/div[@class ='view-footer'] / div[@class ='page_result']").text.strip()
    WebDriverWait(driver,2).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='block-system-main']/div//tbody")))
    all_trows = driver.find_elements_by_xpath("//*[@id='block-system-main']/div/div[@class = 'view-content'] / table / tbody / tr")

    for eachRow in all_trows:
        op_link = eachRow.find_element_by_xpath('//td[position() = 1]/a').get_attribute('href').strip()
        print(op_link)
        
    try:
        WebDriverWait(driver,2).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='block-system-main']/div/div[@class ='item-list']/ul/li")))
        driver.find_element_by_xpath("//*[@id='block-system-main']/div/div[@class = 'item-list']/ul/li[contains(@class, 'pager-next')]/a").click()
        driver.implicitly_wait(5)
        WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath("//*[@id='block-system-main']/div/div[@class ='view-footer'] / div[@class ='page_result']").text.strip()!= current_page_flag)
        page = page + 1
        print(page)
    except NoSuchElementException:
        print('no next button')
        # driver.quit()
        break
这是我的输出

https://www.un.org/Depts/ptd/business-opportunities/EOI/eoiunmiss17882
https://www.un.org/Depts/ptd/business-opportunities/EOI/eoiunmiss17882
https://www.un.org/Depts/ptd/business-opportunities/EOI/eoiunmiss17882
https://www.un.org/Depts/ptd/business-opportunities/EOI/eoiunmiss17882
https://www.un.org/Depts/ptd/business-opportunities/EOI/eoiunmiss17882
1
https://www.un.org/Depts/ptd/business-opportunities/EOI/eoiunficyp17877
https://www.un.org/Depts/ptd/business-opportunities/EOI/eoiunficyp17877
https://www.un.org/Depts/ptd/business-opportunities/EOI/eoiunficyp17877
https://www.un.org/Depts/ptd/business-opportunities/EOI/eoiunficyp17877
https://www.un.org/Depts/ptd/business-opportunities/EOI/eoiunficyp17877
2
https://www.un.org/Depts/ptd/business-opportunities/EOI/eoiescwa17908
https://www.un.org/Depts/ptd/business-opportunities/EOI/eoiescwa17908
https://www.un.org/Depts/ptd/business-opportunities/EOI/eoiescwa17908
https://www.un.org/Depts/ptd/business-opportunities/EOI/eoiescwa17908
https://www.un.org/Depts/ptd/business-opportunities/EOI/eoiescwa17908

我希望在迭代不同的行时,我必须得到不同的href值。

您需要更改此行

op\u link=eachRow。通过xpath('//td[position()=1]/a')查找元素。获取属性('href')。strip()

当您迭代行时,它总是选择DOM树中的第一项

要从DOM树中为需要更改的每一行选择直接子行,请使用

所以你的代码应该是

for eachRow in all_trows:
        op_link = eachRow.find_element_by_xpath('.//td[position() = 1]/a').get_attribute('href').strip()
        print(op_link)

嗨,谢谢你的回复。这对我帮助很大。我在我的控制台中不断收到一条消息,同时作为[1123/152232.645:INFO:console(2435)]“未捕获类型错误:无法将属性'oldValue'设置为null”,来源:(2435)你能告诉我这意味着什么?如何处理它?我想这是一个单独的问题,没有附在你原来的帖子上。如果你发布一个新的问题,并提及你遇到的问题以及你的预期,我将不胜感激。谢谢这只是一个警告,我不明白,这就是我为什么把它放在这里。@TalibDaryabi:我没有执行你的代码。然而,你得到的警告是哪一行?