按一下按钮即可加载评论

时间:2016-11-30 20:22:39

标签: 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来按下按钮并加载内容:

        <div class="card card-block">
        <l class="list" ng-repeat="post in list.posts">
            </br>
            <h4 class="card-title">{{post.title}}</h4>
            <p class="card-text">{{post.body}}</p>
            <!--- the functionality of the below will be added towards end, time permitting-->
            <a href="#/topic" class="btn btn-outline-primary">Like</a>
            <a href="#/topic" class="btn btn-outline-primary">Comment</a>
            </br
        </l>
        </div>
    </div>

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

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()

1 个答案:

答案 0 :(得分:2)

您的按钮位于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()