无法点击包含“>"的下一页按钮标志 [英] Unable to click on the next page button containing ">" sign
问题描述
我已经用 Python 和 Selenium 编写了一些代码来从网站上获取一些电话号码.要查找任何州的内容,必须在相应的搜索框中输入城市名称并按搜索按钮.我以奥兰多"作为城市名称以正确的方式做到了.但是,在按下搜索按钮时,会出现一个通过分页遍历不同页面的文档列表.除了单击下一步按钮之外,我的脚本可以完成所有这些操作.如何修改我的脚本以单击下一页按钮,直到没有下一页按钮为止?提前致谢.
我正在使用的链接:链接.>
我正在尝试使用的脚本:
from selenium import webdriver;导入时间from selenium.webdriver.common.by import By从 selenium.webdriver.support.ui 导入 WebDriverWait从 selenium.webdriver.support 导入 expected_conditions 作为 EC驱动程序 = webdriver.Chrome()等待 = WebDriverWait(驱动程序,10)driver.get("上面的链接")wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input[name='city']"))).send_keys("Orlando")driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".btn-primary"))).click()时间.sleep(5)为真:尝试:link = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".pagination a")))如果 link.text == ">":打印(链接.文本)链接.点击()时间.sleep(5)除了:休息驱动程序退出()
下一页元素位于:
<li class="active"><span>1</span></li><li><a href="javascript:void(0);"onclick="(function(tgt){var rt={};rt=$.parseJSON(unescape('%7B%22fname%22%3A%22%22%2C%22lname%22%3A%22%22%2C%22city%22%3A%22Naples%22%2C%22tcustom11%22%3A%22%22%2C%22icustom12%22%3A%22%22%2C%22uat_1%22%3A%22%22%2C%22icustom43%22%3A%220%22%2C%22near%22%3A%22%22%2C%22dist%22%3A%2210%22%2C%22id%22%3A%2258%22%2C%22lat%22%3A%22%22%2C%22lon%22%3A%22%22%2C%22co%22%3A%22%22%7D'));rt.p=2;soc.ajax('cp','ld','ajax',rt);})(this);return false;">2</a></li><li><a href="javascript:void(0);"onclick="(function(tgt){var rt={};rt=$.parseJSON(unescape('%7B%22fname%22%3A%22%22%2C%22lname%22%3A%22%22%2C%22city%22%3A%22Naples%22%2C%22tcustom11%22%3A%22%22%2C%22icustom12%22%3A%22%22%2C%22uat_1%22%3A%22%22%2C%22icustom43%22%3A%220%22%2C%22near%22%3A%22%22%2C%22dist%22%3A%2210%22%2C%22id%22%3A%2258%22%2C%22lat%22%3A%22%22%2C%22lon%22%3A%22%22%2C%22co%22%3A%22%22%7D'));rt.p=2;soc.ajax('cp','ld','ajax',rt);})(this);return false;">></a></li><li><a href="javascript:void(0);"onclick="(function(tgt){var rt={};rt=$.parseJSON(unescape('%7B%22fname%22%3A%22%22%2C%22lname%22%3A%22%22%2C%22city%22%3A%22Naples%22%2C%22tcustom11%22%3A%22%22%2C%22icustom12%22%3A%22%22%2C%22uat_1%22%3A%22%22%2C%22icustom43%22%3A%220%22%2C%22near%22%3A%22%22%2C%22dist%22%3A%2210%22%2C%22id%22%3A%2258%22%2C%22lat%22%3A%22%22%2C%22lon%22%3A%22%22%2C%22co%22%3A%22%22%7D'));rt.p=2;soc.ajax('cp','ld','ajax',rt);})(this);return false;">»</a></li>
下一页按钮看起来像:
<代码>">"
这个应该可行:
driver = webdriver.Chrome()等待 = WebDriverWait(驱动程序,10)driver.get("http://www.facdl.org/page/find-a-lawyer")wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input[name='city']"))).send_keys("Orlando")wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".btn-primary"))).click()时间.sleep(2)为真:尝试:link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, ">")))链接.点击()等待.直到(EC.staleness_of(链接))除了:休息
我添加了 time.sleep(2)
以等待页面滚动并变为静态.还有 wait.until(EC.staleness_of(link))
等待新的按钮实例创建
I've written some code in Python in combination with Selenium to grab some phone numbers from a website. To find the content of any state it is necessary to enter city names in the appropriate search box and press the search button. I did it the right way with "Orlando" as the city name. However, upon pressing the search button a list of documents come up which are traversing different pages through pagination. My script can do all this except for clicking the next button. How can I mend my script to click on the next page button until no more next page button is left? Thanks in advance.
The link I'm working with: the link.
Script I'm trying with:
from selenium import webdriver;import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("above link")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input[name='city']"))).send_keys("Orlando")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".btn-primary"))).click()
time.sleep(5)
while True:
try:
link = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".pagination a")))
if link.text == ">":
print(link.text)
link.click()
time.sleep(5)
except:
break
driver.quit()
Next page elements are within:
<ul class="pagination">
<li class="active"><span>1</span></li>
<li><a href="javascript:void(0);" onclick="(function(tgt){var rt={};rt=$.parseJSON(unescape('%7B%22fname%22%3A%22%22%2C%22lname%22%3A%22%22%2C%22city%22%3A%22Naples%22%2C%22tcustom11%22%3A%22%22%2C%22icustom12%22%3A%22%22%2C%22uat_1%22%3A%22%22%2C%22icustom43%22%3A%220%22%2C%22near%22%3A%22%22%2C%22dist%22%3A%2210%22%2C%22id%22%3A%2258%22%2C%22lat%22%3A%22%22%2C%22lon%22%3A%22%22%2C%22co%22%3A%22%22%7D'));rt.p=2;soc.ajax('cp','ld','ajax',rt);})(this);return false;">2</a></li>
<li><a href="javascript:void(0);" onclick="(function(tgt){var rt={};rt=$.parseJSON(unescape('%7B%22fname%22%3A%22%22%2C%22lname%22%3A%22%22%2C%22city%22%3A%22Naples%22%2C%22tcustom11%22%3A%22%22%2C%22icustom12%22%3A%22%22%2C%22uat_1%22%3A%22%22%2C%22icustom43%22%3A%220%22%2C%22near%22%3A%22%22%2C%22dist%22%3A%2210%22%2C%22id%22%3A%2258%22%2C%22lat%22%3A%22%22%2C%22lon%22%3A%22%22%2C%22co%22%3A%22%22%7D'));rt.p=2;soc.ajax('cp','ld','ajax',rt);})(this);return false;">></a></li>
<li><a href="javascript:void(0);" onclick="(function(tgt){var rt={};rt=$.parseJSON(unescape('%7B%22fname%22%3A%22%22%2C%22lname%22%3A%22%22%2C%22city%22%3A%22Naples%22%2C%22tcustom11%22%3A%22%22%2C%22icustom12%22%3A%22%22%2C%22uat_1%22%3A%22%22%2C%22icustom43%22%3A%220%22%2C%22near%22%3A%22%22%2C%22dist%22%3A%2210%22%2C%22id%22%3A%2258%22%2C%22lat%22%3A%22%22%2C%22lon%22%3A%22%22%2C%22co%22%3A%22%22%7D'));rt.p=2;soc.ajax('cp','ld','ajax',rt);})(this);return false;">»</a></li>
</ul>
Next page button looks like:
">"
This one should work:
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("http://www.facdl.org/page/find-a-lawyer")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input[name='city']"))).send_keys("Orlando")
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".btn-primary"))).click()
time.sleep(2)
while True:
try:
link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, ">")))
link.click()
wait.until(EC.staleness_of(link))
except:
break
I've added time.sleep(2)
to wait until page is scrolled and became static. Also wait.until(EC.staleness_of(link))
to wait for new button instance creation
这篇关于无法点击包含“>"的下一页按钮标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!