小编Gus*_*bel的帖子

Python Selenium创建循环以单击页面上的链接并按下每个新页面上的按钮

我对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 …
Run Code Online (Sandbox Code Playgroud)

python selenium loops click hyperlink

5
推荐指数
1
解决办法
4013
查看次数

在Python中使用Selenium单击具有相同类名的所有元素

我试图点击网页上的所有"喜欢"按钮.我知道如何点击其中一个,但我希望能够点击它们.它们具有相同的类名,但ID不同.

我是否需要创建某种列表并告诉它单击列表中的每个项目?有没有办法写"全部点击"?

这是我的代码看起来像(我删除了登录代码):

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

browser = webdriver.Firefox()
browser.set_window_size(650, 700)
browser.get('http://iconosquare.com/viewer.php#/tag/searchterm/grid')

mobile = browser.find_element_by_id('open-menu-mobile')
mobile.click()
search = browser.find_element_by_id('getSearch')
search.click()
search.send_keys('input search term' + Keys.RETURN)

#this gets me to the page I want to click the likes
fitness = browser.find_element_by_css_selector("a[href*='fitness/']")
fitness.click()

#here are the different codes I've tried to use to click all of the "like buttons"

#tried to create a list of all elements with "like" in the id and click on all of …
Run Code Online (Sandbox Code Playgroud)

python selenium click selenium-webdriver

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

click ×2

python ×2

selenium ×2

hyperlink ×1

loops ×1

selenium-webdriver ×1