【发布时间】:2021-01-24 05:47:50
【问题描述】:
注意:我在 python 方面经验丰富,但刚开始使用 selenium 和 webscraping。如果这是一个不好的问题,或者我在硒方面的基础知识似乎有问题,请原谅。我在几个小时的搜索中找不到答案,所以我在这里问
目标:提取企业 Yelp 页面中的“About the Business”信息 某些页面在基于“阅读更多”按钮的弹出窗口中包含有关业务信息(例如:https://www.yelp.com/biz/and-pizza-bethesda-bethesda) 某些页面在基于阅读更多按钮的弹出窗口中没有他们的业务信息(例如:https://www.yelp.com/biz/pneuma-fashions-upper-marlboro-3)
问题:无法导航到单击“阅读更多”按钮后出现的“关于业务”弹出窗口并提取其中的文本。
目前的尝试:通过谷歌搜索,我找到了有关如何处理警报弹出窗口或窗口弹出窗口的解释。但是代码不起作用。单击Read More按钮时出现的弹出窗口不会导致window_handles发生变化
import re
# getting all sections of the page
result=driver.find_elements_by_tag_name("section")
About = None
for sec in result:
if sec.text.startswith("About the Business"):
# this pertains only to the About the business section
main_page=driver.current_window_handle
print(main_page) # Returns the current handle
sec.find_element_by_tag_name("button").click()
popup=None
for handle in driver.window_handles: # is an iterable with only one handle
# The only handle present is the main_page handle
print(handle)
if handle!=main_page:
popup = handle
print(popup) # returns None
driver.switch_to.window(popup) # Throws error because popup=None
# THE FOLLOWING SECTION IS NOT EXECUTED BECAUSE OF THE ERROR ABOVE
#////////////////////////////////////////////////////
button_contents=driver.find_elements_by_tag_name("p")
for b in button_contents:
print(b.text) # intended to print text contents
close=driver.find_element_by_tag_name("button")
close.click()
driver.switch_to.window(main_page)
请帮忙
感谢所有阅读此问题并提供建议和答案的人
【问题讨论】:
标签: selenium selenium-webdriver dom web-scraping beautifulsoup