【问题标题】:Looping through rows of a table while clicking links in selenium (python)单击 selenium 中的链接时循环遍历表的行(python)
【发布时间】:2018-07-06 13:05:59
【问题描述】:

示例页面源代码如下所示

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

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

所以我想循环获取每个&lt;tbody data-live="false"&gt; 并单击它。

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

谢谢。

我的代码

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

错误

返回的错误在行:

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

例外

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

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

【问题讨论】:

标签: python selenium web-scraping


【解决方案1】:

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

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']")
for tds in all_tds :
    tds.click()
    #try for one row first and break out
    break

注意:这是克服AttributeError: 'list' object has no attribute 'find_element_by_xpath'try for one row first 错误的基本解决方案


更新

如果您打算循环遍历表格的所有td 元素,那么您必须捕获base url,以便我们可以返回主页以查找 em> 和 click 后面的元素如下:

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)

【讨论】:

  • 感谢您的回复。从我得到的错误来看,那是真的......但是我怎样才能找到列表中的元素呢?
  • 现在所有的 tds 都已经在列表中了。一个一个地迭代它们。 #先尝试一行,然后告诉我状态。
  • 它有效....哇....但问题是它仅适用于第一次点击。即使在添加 driver.back() 之后也不会循环所有 clicks()。我该如何解决?
  • 刷新时出现错误,因为 td 和 table 会过时
  • selenium.common.exceptions.StaleElementReferenceException: 消息: 的元素引用已过时;要么元素不再附加到 DOM,它不在当前框架上下文中,要么文档已被刷新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-29
  • 1970-01-01
  • 2022-11-13
  • 2020-02-17
  • 1970-01-01
  • 1970-01-01
  • 2018-07-20
相关资源
最近更新 更多