python - 使用 selenium/python 点击“下一步”按钮后,Url 不会改变
问题描述
我从这个 url 抓取数据,然后单击Next
按钮并等待 10 秒,然后使用 requests 和 bs4 抓取下一页,但 url 没有改变,所以我最终只抓取了两次原始页面数据。我已经尝试WebDriverWait
直到第一页上的元素变得陈旧以及尝试使用请求直接获取 xhr log api 调用(但是我不精通 ajax)并且找不到解决方案。这是代码:
loop = True
while loop:
try:
current_url = driver.current_url
next_btn = WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH, '//button[text()="Next"]')))
actions = ActionChains(driver)
actions.move_to_element(next_btn).perform()
if next_btn:
next_btn.click()
except Exception as e:
current_url = driver.current_url
loop = False
print(e,f"somewhere in {current_url} while loop")
else:
time.sleep(10)
next_page = driver.current_url
get_page_content(next_page)
break
这是第一页的 URL:https ://www.hunterdouglas.com/locator/results?address=San%20Ramon&country=US&source=
任何方向将不胜感激!谢谢!
解决方案
对于任何有兴趣的人,我只使用硒就可以做到这一点。这是代码,参数data
只是我要提交的城市名称master_function(data)
def get_links(page):
for p in page:
for l in p.find_elements_by_tag_name("a"):
link = l.get_attribute('href')
if link != None:
link_split = link.split('\n')
for l in link_split:
if "http" in link:
test_list.append(link)
def master_function(data):
for d in data:
base_url = "https://www.hunterdouglas.com/locator"
driver.get(base_url)
url = pop_up_one(driver)
submit(url,driver,d)
loop = True
while loop:
try:
current_url = driver.current_url
next_btn = WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH, '//button[text()="Next"]')))
actions = ActionChains(driver)
actions.move_to_element(next_btn).perform()
if next_btn:
next_btn.click()
except Exception as e:
current_url = driver.current_url
loop = False
print(e,f"somewhere in {current_url} while loop")
else:
time.sleep(1)
page = WebDriverWait(driver,5).until(EC.presence_of_all_elements_located((By.XPATH, '//div[@id="loc-results"]')))
get_links(page)
推荐阅读
- java - 如何在Java中对键上的地图列表进行排序?
- abap - 如何在 SAP ABAP 类中知道当前浏览器的 URL
- mysql - Mysql-Workbench 表浏览器窗格丢失
- python-2.7 - 从其他目录调用 python 脚本函数
- java - 如何将mongodb视图与pageable和querydsl结合起来?
- sql - SQL 将行转换为列并填充值
- java - setOnClickListener Android Studio(制作计数器)
- javascript - 通过代理的 Google Places API 超时
- cypress - 比较赛普拉斯中的动态文本
- reactjs - 在反应图中具有相同x轴的多个y轴值