selenium - 按下按钮加载评论

标签 selenium xpath web-scraping scrapy

我正在尝试从此网站上抓取评论:

https://www.bbb.org/sacramento/business-reviews/heating-and-air-conditioning/elite-heating-air-conditioning-in-elk-grove-ca-47012326/reviews-and-complaints

但是评论的内容没有被scrapy加载。 然后我尝试使用 selenium 来按下按钮并加载内容:

url = 'https://www.bbb.org/losangelessiliconvalley/business-reviews/plumbers/bryco-plumbing-in-chatsworth-ca-13096711/reviews-and-complaints'
driver_1 = webdriver.Firefox()
driver_1.get(url)
content = driver_1.page_source
REVIEWS_BUTTON = '//*[@class="button orange first"]'
button = driver_1.find_element_by_xpath(REVIEWS_BUTTON)
button.click()

但是 selenium 无法从上面的 xapth 中找到按钮,我收到以下错误:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//*[@class=\"button orange first\"]"}

最佳答案

您的按钮位于 iframe 内,因此您需要先切换到它,然后再处理该按钮:

REVIEWS_BUTTON = '//*[@class="button orange first"]'
driver_1.switch_to_frame('the_iframe') 
button = driver_1.find_element_by_xpath(REVIEWS_BUTTON)
button.click()
driver.switch_to_default_content()

关于selenium - 按下按钮加载评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40897468/

相关文章:

Selenium - 在元素中搜索元素

python - <br/> 标签之间节点的 Scrapy 选择器

go - 抓取数据时拒绝连接

python - 无法以正确的方式使用 "explicit wait"

python - 阻止请求 URL selenium firefox python

sql-server - SQL 使用 OpenXML 检索多个元素

python - 需要网页抓取授权

python - Beautifulsoup findAll,如何获取第二条文本

selenium - 在 selenium webdriver 中将 xpath 转换为 css 无法正常工作?

ruby-on-rails - nokogiri 从 xml 到数组的多个元素