Python Selenium创建循环,单击页面上的链接,并在每个新页面上按下按钮
我对Python和Selenium还不太熟悉,但我已经开始学习了。我一直在用谷歌搜索如何解决这个编码问题,但找不到确切的解决方案 我试图完成的是单击页面上的所有用户名链接,单击我进入的页面上的follow按钮,然后返回到原始页面,并对其余的用户名链接执行相同的操作 基本上,我想创建一个循环来实现这一点:Python Selenium创建循环,单击页面上的链接,并在每个新页面上按下按钮,python,loops,selenium,hyperlink,click,Python,Loops,Selenium,Hyperlink,Click,我对Python和Selenium还不太熟悉,但我已经开始学习了。我一直在用谷歌搜索如何解决这个编码问题,但找不到确切的解决方案 我试图完成的是单击页面上的所有用户名链接,单击我进入的页面上的follow按钮,然后返回到原始页面,并对其余的用户名链接执行相同的操作 基本上,我想创建一个循环来实现这一点: 单击第一个用户名 点击follow按钮 返回上一页 单击第二个用户名 点击follow按钮 返回上一页 等等。。。。。通过每个环节 以下是我当前的代码和我迄今为止尝试的代码: from
- 点击follow按钮
- 返回上一页
- 点击follow按钮
- 返回上一页
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()
程序完成,屏幕上什么也没发生。也没有错误消息
我必须更改什么才能单击初始页面上的每个链接,并单击链接带我到的页面上的按钮
这里有一个类似问题的链接
非常感谢您的帮助。很长时间了,但如果有人还在检查同类问题,请发布答案
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()
那么你的代码怎么了?有错误吗?或者它的行为不符合预期?如果是,具体如何?单击浏览器上的[enter]后,什么也不会发生。返回()行。它转到另一个空白处,如“…”。在那之后我点击了回车,网页上什么也没发生,但是没有出现错误消息。嗯。。这很奇怪:)你能尝试添加
浏览器吗?在textsave之后隐式地等待(5)
。写(text+“\n\n”)
行并再次检查吗?我不知道如何使用你提到的测试代码。我的问题是如何将该代码更改为单击链接然后单击按钮,而不是单击链接并保存文本您是否尝试过使用browser.get(网页)而不是back()返回起点?