单击 selenium 中的链接时循环遍历表的行(python) [英] Looping through rows of a table while clicking links in selenium (python)

查看:49
本文介绍了单击 selenium 中的链接时循环遍历表的行(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例页面源代码如下

<table class="foot-market"><tbody data-live="false"><td class='today-name'/></tbody><tbody data-live="false"><td class='today-name'/></tbody><tbody data-live="false"><td class='today-name'/></tbody></表<table class="foot-market"><tbody data-live="false"><td class='today-name'/></tbody><tbody data-live="false"><td class='today-name'/></tbody><tbody data-live="false"><td class='today-name'/></tbody></表

说明你好,所以让我们开始吧.如上所示,我正在与之交互的代码片段位于 <div class='div1'> 中.<td class='today-name'> 是可点击的,一旦点击它就会呈现一个页面(感兴趣的页面)

所以我想循环获取每个 <tbody data-live="false"> 并单击它.

从我的研究中,我没有发现任何类似的东西,但我发现了一些有趣的东西,但没有任何帮助.感谢您提供的所有帮助.

谢谢.

我的代码

导入时间进口硒从硒导入网络驱动程序从 selenium.webdriver.common.keys 导入密钥从 selenium.webdriver.support.ui 导入 WebDriverWait从 selenium.webdriver.firefox.options 导入选项从 BeautifulSoup 导入 BeautifulSoup#一些进口是为无头火狐所以不要介意他们#for.testing. purposes.only驱动程序 = webdriver.Firefox()url = '这里://code.above.com/'driver.get(url)#循环硒中的行table = driver.find_elements_by_xpath("//table[@class='foot-market']")对于 table.find_element_by_xpath("//tbody[@data-live='false']") 中的行:row.find_element_by_xpath("//td[@class='today-name']").click()#先尝试一行# driver.back()休息

错误

返回的错误在一行:

对于 table.find_element_by_xpath("//td[@data-live='false']") 中的行:

例外:

<块引用>

AttributeError: 'list' 对象没有属性 'find_element_by_xpath'

这是有道理的,但我如何实现这一点...

解决方案

您所看到的错误说明了一切:

 for row in table.find_element_by_xpath("//td[@data-live='false']"): AttributeError: 'list' object has no attribute 'find_element_by_xpath'

以下行容易出错:

 for row in table.find_element_by_xpath("//td[@data-live='false']"):

根据您的前一行:

table = driver.find_elements_by_xpath("//table[@class='foot-market']")

table 是一个 List,您不能在 List 上调用 find_element_by_xpath().find_element_by_xpath() 可以在 webdriver 实例或 webelement 上调用.

您的工作代码如下:

all_tds = driver.find_elements_by_xpath("//table[@class='foot-market']//td[@class='today-name']")对于 all_tds 中的 tds :tds.click()#先尝试一行然后爆发休息

<块引用>

注意:这是克服错误的基本解决方案AttributeError: 'list' object has no attribute 'find_element_by_xpath'先尝试一行

<小时>

更新

如果您打算遍历表的所有 td 元素,那么您必须捕获 base url 以便我们可以返回到主页以 find点击后面的元素如下:

base_url = driver.current_urlcount_all_tds = len(driver.find_elements_by_xpath("//table[@class='foot-market']//td[@class='today-name']"))对于范围内的 td(count_all_tds):driver.find_elements_by_xpath("//table[@class='foot-market']//td[@class='today-name']")[td].click()# 其他所需操作的代码块,最后重定向到 base_urldriver.get(base_url)

The sample page source looks like this

<div class='div1'>
  <table class="foot-market">
    <tbody data-live="false">
      <td class='today-name'/>
    </tbody>
    <tbody data-live="false">
      <td class='today-name'/>
    </tbody>
    <tbody data-live="false">
      <td class='today-name'/>
    </tbody>
  </table

  <table class="foot-market">
    <tbody data-live="false">
      <td class='today-name'/>
    </tbody>
    <tbody data-live="false">
      <td class='today-name'/>
    </tbody>
    <tbody data-live="false">
      <td class='today-name'/>
    </tbody>
  </table
</div>

Explanation Hi, So let's get to it. As shown above the code snippet I'm interacting with is housed in a <div class='div1'>. The <td class='today-name'> is clickable and once clicked it renders a page(the page of interest)

so I would like to loop through getting each <tbody data-live="false"> and click it.

From my research I haven't found anything similar but I have found interesting stuff but nothing to help. I appreciate all help given.

Thanks.

my code

import time
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.firefox.options import Options
from BeautifulSoup import BeautifulSoup

#some of the imports are for headless firefox so don't mind them
#for.testing.purposes.only
driver = webdriver.Firefox()

url = 'here://code.above.com/'
driver.get(url)

#looping rows in selenium
table = driver.find_elements_by_xpath("//table[@class='foot-market']")

for row in table.find_element_by_xpath("//tbody[@data-live='false']"):
    row.find_element_by_xpath("//td[@class='today-name']").click()

    #try for one row first
    # driver.back()
    break

Error

The error returned is in line:

for row in table.find_element_by_xpath("//td[@data-live='false']"):

Exception:

AttributeError: 'list' object has no attribute 'find_element_by_xpath'

which makes sense and all, but how do I achieve this...

解决方案

The error you are seeing says it all :

for row in table.find_element_by_xpath("//td[@data-live='false']"): AttributeError: 'list' object has no attribute 'find_element_by_xpath'

The following line is error prone :

for row in table.find_element_by_xpath("//td[@data-live='false']"):

As per your previous line :

table = driver.find_elements_by_xpath("//table[@class='foot-market']")

table is a List and you can't invoke find_element_by_xpath() on a List. find_element_by_xpath() can be invoked either on webdriver instance or on a webelement.

Your working code will be as follows :

all_tds = driver.find_elements_by_xpath("//table[@class='foot-market']//td[@class='today-name']")
for tds in all_tds :
    tds.click()
    #try for one row first and break out
    break

Note : This is a basic solution to overcome your error AttributeError: 'list' object has no attribute 'find_element_by_xpath' and to try for one row first


Update

If you intend to loop through all the td elements of the table, then you have to capture the base url so we can revert back to the main page to find and click the subsequent elements as follows :

base_url = driver.current_url
count_all_tds = len(driver.find_elements_by_xpath("//table[@class='foot-market']//td[@class='today-name']"))
for td in range(count_all_tds):
    driver.find_elements_by_xpath("//table[@class='foot-market']//td[@class='today-name']")[td].click()
    # code block for other required actions and finally redirect to base_url
    driver.get(base_url)

这篇关于单击 selenium 中的链接时循环遍历表的行(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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