[英]How do I iterate through each google search page using Selenium Python,but its not happening
I am trying to iterate through each page, but below code is not working for me. 我试图遍历每个页面,但是下面的代码对我不起作用。
pages=driver.find_elements_by_xpath("//*[@id='nav']/tbody/tr/td/a")
print len(pages)
counter=1
for page in pages:
counter+=1
page.click()
Your code will run successfully only for the first time, aka it will click on the 2nd page and then it will throw a Stale Element Reference Exception on this line - 您的代码将仅首次成功运行,也就是单击第二页,然后在此行上引发Stale Element Reference Exception-
page.click()
Now, why is that? 现在,为什么呢? Its because the
page
WebElement is nothing but a member of the pages
list of your elements which you identified before clicking once. 这是因为
page
WebElement只是单击之前确定的元素pages
列表的成员。 Since after clicking the pagination button once, the DOM has changed ,the reference to the element you earlier located no longer holds significance. 由于单击一次分页按钮后,DOM已更改 ,因此对您之前位于的元素的引用不再具有意义。
To solve this, you need to keep finding the pagination button again and again everytime the DOM changes ie everytime you click on the pagination buttons. 为了解决这个问题,您需要每次DOM更改时(即每次单击分页按钮时)一次又一次找到分页按钮。 A simple solution would be to use your
counter
variable to iterate through your list. 一个简单的解决方案是使用您的
counter
变量来遍历列表。 Here is the complete code - 这是完整的代码-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path=r'//path to driver')
driver.get("google url")
driver.find_element_by_id("lst-ib").send_keys("search")
driver.find_element_by_id("lst-ib").send_keys(Keys.ENTER)
driver.maximize_window()
pages=driver.find_elements_by_xpath("//*[@id='nav']/tbody/tr/td/a")
counter=1
for page in pages:
pages=driver.find_elements_by_xpath("//*[@id='nav']/tbody/tr/td/a")
counter+=1
pages[counter].click()
An alternate (and better) solution would be to identify the pagination buttons by their text - 另一种(更好的)解决方案是通过按钮的文字来识别分页按钮-
pages=driver.find_elements_by_xpath("//*[@id='nav']/tbody/tr/td/a")
counter=2 #starting from 2
for page in pages:
driver.find_element_by_xpath("//a[text() = '" + str(counter) + "']").click()
counter+=1
You could also, try to press the 'Next' button: 您也可以尝试按“下一步”按钮:
pages=driver.find_elements_by_xpath("//*[@id='nav']/tbody/tr/td/a")
counter=2 #starting from 2
for page in pages:
driver.find_element_by_xpath("//span[text()='Next']").click()
counter+=1
EDIT - 编辑-
I fixed your final code. 我修正了您的最终代码。 I renamed some variables so that you don't get confused and replaced your implicit waits with explicit waits .
我重命名了一些变量,以免引起混淆,并用显式等待替换了隐式等待 。
import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
class GoogleEveryFirstLink(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path=r'D:\Test automation\chromedriver.exe')
self.driver.get("http://www.google.com")
def test_Hover_Facebook(self):
driver = self.driver
self.assertIn("Google",driver.title)
elem=driver.find_element_by_id("lst-ib")
elem.clear()
elem.send_keys("India")
elem.send_keys(Keys.RETURN)
page_counter=2
links_counter=1
wait = WebDriverWait(driver,20)
wait.until(EC.element_to_be_clickable((By.XPATH,"(//h3[@class='r']/a)[" + str(links_counter) + "]")))
pages=driver.find_elements_by_xpath("//*[@id='nav']/tbody/tr/td/a")
elem1=driver.find_elements_by_xpath("//h3[@class='r']/a")
print len(elem1)
print len(pages)
driver.maximize_window()
for page in pages:
for e in elem1:
my_link = driver.find_element_by_xpath("(//h3[@class='r']/a)[" + str(links_counter) + "]")
print my_link.text
my_link.click()
driver.back()
links_counter+=1
my_page = driver.find_element_by_xpath("//a[text() = '" + str(page_counter) + "']")
my_page.click()
page_counter+=1
def tearDown(self):
self.driver.close()
if __name__=="__main__":
unittest.main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.