2014-09-24 2 views
1

Я пытаюсь моделировать OnClick событие в тэгеНевозможно смоделировать OnClick JavaScript внутри тега <span> с использованием Selenium WebDriver, питона

пролет класс = "taLnk hvrIE6 tr165579546 moreLink ulBlueLinks" OnClick = "ta.util.cookie. setPIDCookie (2247); ta.call ('ta.servlet.Reviews.expandReviews', событие это, 'review_165579546', '1', 2247) "> больше

Это используется, чтобы увидеть больше текста под этим link.Я использую selenium webdriver и python для имитации этого события автоматически для этой веб-страницы http://www.tripadvisor.in/Hotel_Review-g297586-d1154547-Reviews-Rainbow_International_Hotel-Hyderabad_Telangana.html этой страницей.

Может ли кто-либо поделиться общим фрагментом кода, чтобы активировать это событие javascript, чтобы страница загружалась, и я могу видеть весь текст под этой ссылкой автоматически ... Я попытался использовать параметр click() selenium webdriver, но он не Работа.

ответ

0

Это сделал трюк для меня:

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

Вот идея, что вы можете начать с:

  • перебрать все отзывы на странице (Div элементов с id начиная с review_)
  • для каждого обзора, нажмите на More ссылка если есть (span with moreLink class na я)
  • после небольшой задержки, получить полный текст обзора

Вот реализация:

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

Надеемся, что это сделает вас более ясными.

Смежные вопросы