Python Selenium创建循环以单击页面上的链接,然后在每个新页面上按按钮 [英] Python Selenium create loop to click through links on a page and press button on each new page

查看:1207
本文介绍了Python Selenium创建循环以单击页面上的链接,然后在每个新页面上按按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python和Selenium还是很陌生,但是开始学习它.我一直在搜寻如何解决此编码问题,但找不到确切的解决方案.

I'm fairly new to Python and Selenium, but starting to pick it up. I've been googling how to solve this coding issue, but can't find the exact solution.

我要完成的工作是单击页面上的所有用户名链接,单击进入该页面后的跟随"按钮,然后返回到原始页面,并对其余用户名执行相同的操作链接.

What I'm trying to accomplish is click all of the username links on a page, click the follow button on the page that I'm taken to, then return to the original page and do the same for rest of the username links.

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

Basically, I want to create a loop that does this:

  1. 单击第一个用户名
    • 点击关注按钮
    • 返回上一页
  1. click on the first username
    • click on the follow button
    • go back to the previous page
  • 点击关注按钮
  • 返回上一页

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

ETC..... through each link

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

Here is my current code and what I've tried so far:

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

程序完成,屏幕上无任何显示.也没有错误消息.

The program completes and nothing happens on the screen. No error message either.

要更改什么以单击初始页面上的每个链接,然后单击链接将我带到的页面上的按钮?

What must I change to click each link on the initial page and click a button on the pages that the links take me to?

这里是类似问题的链接. 使用Selenium Webdriver(Python)遍历链接

Here's a link to a similar issue. Loop through links using Selenium Webdriver (Python)

非常感谢您的帮助.

推荐答案

已经很长时间了,但是如果有人仍在检查相同种类的问题,只是发布答案.

It's been long time but just posting the answer, if someone still checking for the same kind issue some point.

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

这篇关于Python Selenium创建循环以单击页面上的链接,然后在每个新页面上按按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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