本文介绍了无法模拟< span>内的onclick javascript.使用Selenium Webdriver标记,python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to simulate onclick event at the following tag

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)>更多

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)">More

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

This is used to see more text under this link.I am using selenium webdriver and python for simulating this event automatically for this webpage http://www.tripadvisor.in/Hotel_Review-g297586-d1154547-Reviews-Rainbow_International_Hotel-Hyderabad_Telangana.html this webpage.

任何人都可以共享一个代码段以激活此javascript事件,以便页面加载,并且我可以自动看到该链接下的全文...我尝试使用Selenium Webdriver的click()选项,但它不起作用

could anyone pls share a code snippet inorder to actiavet this javascript event so that page loads up and I can see whole text under that link automatically...I tried using click() option of selenium webdriver but it doesn't work.

推荐答案

以下是您可能以以下内容开头的想法:

Here's the idea that you may start with:

  • 遍历页面上的所有评论(以id开头的id的div元素)
  • 对于每个评论,单击More链接(如果存在)(跨度为moreLink类名)
  • 稍作延迟后,获得完整的评论文字
  • iterate over all reviews on the page (div elements with id starting with review_)
  • for every review, click on the More link if it is present (span with moreLink class name)
  • after a small delay, get the full review text

这是实现:

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 "----"

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

Prints (the output contains all text inside each review including username and date):

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.
----
...

希望可以让您更清楚地看到事情.

Hope that makes things more clear for you.

这篇关于无法模拟< span>内的onclick javascript.使用Selenium Webdriver标记,python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 11:10