【问题标题】:Python Selenium create loop to click through links on a page and press button on each new pagePython Selenium 创建循环以单击页面上的链接并在每个新页面上按下按钮
【发布时间】:2015-09-30 10:08:31
【问题描述】:

我对 Python 和 Selenium 还很陌生,但开始学习它。我一直在谷歌搜索如何解决这个编码问题,但找不到确切的解决方案。

我想要完成的是单击页面上的所有用户名链接,单击我被带到的页面上的关注按钮,然后返回原始页面并对其余用户名执行相同操作链接。

基本上,我想创建一个循环来执行此操作:

  1. 点击第一个用户名
    • 点击关注按钮
    • 返回上一页
  2. 点击第二个用户名
    • 点击关注按钮
    • 返回上一页

ETC.....通过每个链接

这是我当前的代码以及到目前为止我尝试过的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()
browser.get('thewebpage')

search = browser.find_element_by_id('getSearch')
search.click()
search.send_keys('searchitem' + Keys.RETURN)

searchitem = browser.find_elements_by_class_name("name")[0]
searchitem.click()
#I am now on the page where it shows the users

#this is where I'm getting stuck
#here's the first code I tried
links = browser.find_elements_by_link_text("#/user/")
        for link in links:
            link.click()
            follow = browser.find_element_by_class_name("followAction")
            browser.back()

#here's the second code I tried
import selenium.webdriver.support.ui as UI

def test(self):
    driver = self.driver
    wait = UI.WebDriverWait(driver, 5000)
    links = driver.find_elements_by_link_text("#/user/")
    for link in links:
        link.click()
        follow = driver.find_element_by_class_name("followAction")
        follow.click()
        driver.implicityly_wait(5)
        driver.back()

程序完成,屏幕上没有任何反应。也没有错误信息。

单击初始页面上的每个链接并单击链接将我带到的页面上的按钮,我必须进行哪些更改?

这是一个类似问题的链接。 Loop through links using Selenium Webdriver (Python)

非常感谢您的帮助。

【问题讨论】:

  • 那么你的代码有什么问题?有什么错误吗?或者它的行为不像预期的那样?如果是这样 - 究竟如何?
  • 在 browser.back() 行上单击 enter 后没有任何反应。它进入另一个空白,例如“...”。之后我点击回车,网页没有任何反应,但没有弹出错误消息。
  • 嗯.. 很奇怪:) 你可以尝试在textsave.write(text+"\n\n") 行之后添加browser.implicitly_wait(5) 并再次检查吗?
  • 我不知道如何使用您所指的测试代码。我的问题是如何将该代码更改为单击链接然后单击按钮而不是单击链接并保存文本的代码
  • 您是否尝试过仅使用 browser.get(thewebpage) 而不是 back() 来返回您的起点?

标签: python loops selenium hyperlink click


【解决方案1】:

已经很长时间了,但只是发布答案,如果有人仍然在某个时候检查相同类型的问题。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
# need the below imports to work with Explicit wait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

browser = webdriver.Firefox()
browser.get('thewebpage')

search = browser.find_element_by_id('getSearch')
search.click()
search.send_keys('searchitem' + Keys.RETURN)

searchitem = browser.find_elements_by_class_name("name")[0]
searchitem.click()

# Here is the logic that we have to update

# Get number of users rather than the users.
userElems = len(browser.find_elements_by_link_text("#/user/"))

# iterate through each user by using the index
  # if you try to use the find_elements as shown in OP, you will get StaleElement Exception
  # because the user elements references will be refreshed when navigated to next page and
  # load back (so we have to find the elements based on index on the page every time)

for userNum in range(1,userElems):
    # this below explicit wait will make sure the script will wait max 30 sec for the next user to be clicked
    user = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"(#/user/)[" + str(userNum) + "]")))
    # scroll user into view
    user.location_once_scrolled_into_view
    # click on user
    user.click()
    # click on follow link
    follow = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"followAction")))
    follow.click()
    # click on browser back button
    browser.back()

【讨论】:

    猜你喜欢
    • 2020-02-16
    • 2013-11-18
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    相关资源
    最近更新 更多