我正在尝试从此网站上抓取评论:
但是评论的内容没有被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/