为了账号安全,请及时绑定邮箱和手机立即绑定

努力单击带有 Selenium 的加载更多按钮

努力单击带有 Selenium 的加载更多按钮

MYYA 2023-03-08 15:52:02
我计划构建一个同时使用 Selenium 和 BeautifulSoup 的抓取工具。我正在努力单击带有硒的加载更多按钮。我已经设法检测到按钮,滚动到它等等 - 似乎无法找到连续点击按钮的方法。关于如何通过这个障碍有什么建议吗?from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.common.exceptions import TimeoutException, NoSuchElementExceptionfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECimport time, requestsfrom bs4 import BeautifulSoupdef search_agent(zip):    location = bot.find_element_by_name('hheroquotezip')    time.sleep(3)    location.clear()    location.send_keys(zip)    location.submit()def load_all_agents():    # click more until no more results to load    while True:        try:            #more_button = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'results.length'))).click()            more_button = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="searchResults"]/div[3]/button'))).click()        except TimeoutException:            break    # wait for results to load    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.seclection-result .partners-detail')))    print ("Complete")    bot.quit()#define Zip for search queryzip = 20855bot = webdriver.Safari()wait = WebDriverWait(bot, 10)#fetch agents pagebot.get('https://www.erieinsurance.com/find-an-insurance-agent')search_agent(zip)load_all_agents()使用上述方法,控制台会吐出以下错误:[错误] 拒绝加载https://9203275.fls.doubleclick.net/activityi;src=9203275;type=agent0;cat=agent0;ord=7817740349177;gtm=2wg783;auiddc=373080108.1594822533;~oref=https%3A %2F%2Fwww.erieinsurance.com%2Ffind-an-insurance-agent-results%3Fzipcode%3D20855 ? 因为它没有出现在内容安全策略的 frame-src 指令中。[错误] 拒绝连接到https://api.levelaccess.net/analytics/3.0/results因为它没有出现在 Content Security Policy 的 connect-src 指令中。
查看完整描述

1 回答

?
青春有我

TA贡献1784条经验 获得超8个赞

创建一个答案来张贴几张图片。

  • 当我在 chrome 中运行附加的脚本时,它工作正常。

  • 当@furas 在 firefox 中做同样的事情时,他得到了同样的结果

  • 我连续运行同一个脚本 10 次,没有被拒绝。

我根据错误注意到的是 iframe 似乎对浏览器敏感:

在 Chrome 中,此标头包含铬脚本:

//img1.sycdn.imooc.com//64083ed50001db5b09850635.jpg

在 Firefox 中它不包含脚本:

//img1.sycdn.imooc.com//64083ee90001422206590182.jpg

看看你在 safari 中手动得到了什么。

一个简单的答案可能是不使用 safari - 使用 chrome 或 FF。那是一个选择吗?(如果它必须是 safari 就说,我会再看一遍。)

最后 - 一些快速的附加说明。

该站点使用的是角度,因此如果您在同步方面遇到困难,则可能需要考虑量角器。(量角器有助于一些脚本同步功能)

还值得一提的是 - 不要觉得您必须登陆主页然后以用户身份导航。将您的 URL 更新到搜索结果页面并输入邮政编码,这样可以节省一些时间:

https://www.erieinsurance.com/find-an-insurance-agent-results?zipcode=20855

[编辑/更新] 这是同一回事吗?https://github.com/SeleniumHQ/selenium/issues/458 2016 年围绕“内容安全策略”关闭的错误 - 记录为苹果的东西。


查看完整回答
反对 回复 2023-03-08
  • 1 回答
  • 0 关注
  • 106 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信