python - 尝试使用 selenium 单击查看更多选项卡
问题描述
我一直在使用 selenium 来抓取网站以检索一些信息。当我单击它时,该信息隐藏在使用 javascript 显示的查看更多选项卡后面。我尝试了许多不同的方法来获取可见的信息。它似乎不起作用。
我尝试使用动作链和常规的 xpath 方法将功能链接在一起,但它似乎仍然没有单击所有其他信息,并且按钮文本被打印到控制台而不是被单击。
def grabDetails(self):
facts = self.browser.find_elements_by_xpath("//section[@id='hdp-content']/main/div/div[4]")
for fact in facts:
details = fact.text
print(details)
def moreFeatures(self):
view_more_elements = WebDriverWait(self.browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(text(),'See More Facts and Features')]")))
# features.click()
ActionChains(view_more_elements).double_click().preform()
# self.browser.execute_script('arguments[0].scrollIntoView(true);', features)
我试图从这个页面打印出来的信息! 这是我试图抓取的 zillow 页面
它下面的查看更多部分
解决方案
您设置view_more_elements
为WebDriverWait
对象而不是WebElement
. 这将防止对象被点击。您只需要WebDriverWait
按照自己的调用运行,然后.click()
是元素。
view_more_xpath = '//div[@class="read-more zsg-centered"]/a'
view_more_elements = WebDriverWait(self.browser, 20).until(EC.visibility_of_element_located((By.XPATH, view_more_xpath))
self.browser.find_element_by_xpath(view_more_xpath).click()
编辑:您实际上不需要设置WebDriverWait
为view_more_elements
. 你可以这样做:
view_more_xpath = '//div[@class="read-more zsg-centered"]/a'
WebDriverWait(self.browser, 20).until(EC.visibility_of_element_located((By.XPATH, view_more_xpath))
self.browser.find_element_by_xpath(view_more_xpath).click()
推荐阅读
- repast-simphony - Repast:无法从 Context Builder 中的另一个类调用方法
- android - 错误:程序类型已经存在:org.kobjects.base64.Base64 同时生成签名的 APK
- reactjs - React 中的 JSON 数据对象处理
- amazon-web-services - 如何简单地在 AWS ECS 中“回收/重启”正在运行的任务而不改变图像/源代码?
- reactjs - 当我仅单击其中一个项目时,如何保持 Material-ui Select 处于打开状态
- java - 由于 AWS ECS 容器中的页面崩溃,Selenium Webdriver 失败
- apache-spark - 如何使用 Spark Structured Streaming 配置 Confluent 的 Schema Registry 和 Avro 序列化程序?
- java - 如何创建 Spring Data JPA 可以解析的 URL 以包含使用 nulls_first 的排序?
- python - 从用户那里获取数据并将其保存在数据库中
- android - 使用多个密钥库文件生成多个 apk