【问题标题】:Unable to simulate onclick javascript inside <span> tag using Selenium webdriver,python无法使用 Selenium webdriver,python 在 <span> 标记内模拟 onclick javascript
【发布时间】:2014-11-18 23:48:14
【问题描述】:

我正在尝试在以下标记处模拟 onclick 事件

span class="taLnk hvrIE6 tr165579546 moreLink ulBlueLinks" onclick="ta.util.cookie.setPIDCookie(2247);ta.call('ta.servlet.Reviews.expandReviews',event,this,'review_165579546', ' 1', 2247)"> 更多

这用于在此链接下查看更多文本。我正在使用 selenium webdriver 和 python 为该网页自动模拟此事件 http://www.tripadvisor.in/Hotel_Review-g297586-d1154547-Reviews-Rainbow_International_Hotel-Hyderabad_Telangana.html这个网页。

任何人都可以分享一个代码 sn-p 以激活这个 javascript 事件,以便页面加载,我可以自动看到该链接下的整个文本...我尝试使用 selenium webdriver 的 click() 选项,但它没有没用。

【问题讨论】:

    标签: javascript python selenium web-scraping beautifulsoup


    【解决方案1】:

    您可以从以下想法开始:

    • 遍历页面上的所有评论(以id 开头的div 元素review_
    • 对于每条评论,点击More 链接如果它存在(跨越moreLink 类名)
    • 稍等片刻后,获取完整的评论文本

    下面是实现:

    import time
    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException, TimeoutException
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Firefox()
    driver.get("http://www.tripadvisor.com/Hotel_Review-g297586-d1154547-Reviews-Rainbow_International_Hotel-Hyderabad_Telangana.html")
    
    for review in driver.find_elements_by_xpath('//div[starts-with(@id, "review_")]'):
        try:
            more = WebDriverWait(review, 3).until(EC.presence_of_element_located((By.CLASS_NAME, 'moreLink')))
            if more.is_displayed():
                more.click()
                time.sleep(1)
        except (NoSuchElementException, TimeoutException):
            pass
    
        full_review = review.find_element_by_class_name('dyn_full_review')
        print full_review.text
        print "----"
    

    打印(输出包含每个评论中的所有文本,包括用户名和日期):

    Mustufa W
    1 review
    “Horrible”
    Reviewed August 15, 2014
    I checked on price was high but cracked a deal
    Poor hygiene in corridor & so in rooms. Washroom pipes were leaking. AC water dripping in washroom.
    First I was given a room to which my surprise found window pane was missing after complaining room got changed.
    They are cheating ppl only good thing abt hotel is the spot & is damn opposite Nilofer cafe which serves delicious tea,coffee & bakery products.
    There is a guy named khwaja who was very helpful. Front @ reception guy was stupid..in one midnight , power went off & to my surprise they don't have power back up..
    Stayed August 2014, traveled as a couple
    Less
    Was this review helpful?
    Yes
    Ask Mustufa W about Rainbow International Hotel
    This review is the subjective opinion of a TripAdvisor member and not of TripAdvisor LLC.
    ----
    mrravi4u
    Bangalore, India
    2 reviews
    13 helpful votes
    “Good Hotel”
    Reviewed April 23, 2014
    I stayed there 2 days i got good services. Rainbow Is good hotel in Hyderabad. there are very homely environment and hosting services was supper. it is also in center of hyderabad city so for convenience is is better place to stay.
    Room Tip: Office Meeting
    See more room tips
    Stayed March 2014, traveled with friends
    Value
    Location
    Sleep Quality
    Rooms
    Cleanliness
    Service
    Was this review helpful?
    Yes
    13
    Ask mrravi4u about Rainbow International Hotel
    This review is the subjective opinion of a TripAdvisor member and not of TripAdvisor LLC.
    ----
    ...
    

    希望这能让你更清楚。

    【讨论】:

      【解决方案2】:

      这对我有用:

      from selenium import webdriver
      
      url = 'http://www.tripadvisor.in/Hotel_Review-g297586-d1154547-Reviews-Rainbow_International_Hotel-Hyderabad_Telangana.html'
      
      browser = webdriver.Firefox()
      browser.get(url)
      li = browser.find_element_by_css_selector('#PERSISTENT_TAB_HR .tabs_pers_content li:nth-child(2)')
      li.click()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-26
        • 2017-09-17
        • 2015-08-04
        • 1970-01-01
        • 2019-02-27
        • 1970-01-01
        • 2016-04-24
        • 1970-01-01
        相关资源
        最近更新 更多