以下是测试用例:
#!/usr/bin/env python
from selenium import webdriver
from lxml.cssselect import CSSSelector
import selenium.webdriver.support.ui as ui
import time
def test(urlx):
br = webdriver.PhantomJS('phantomjs')
start_time = time.time()
br.get(url)
restaurant_url_sel = u'.restaurants a'
ta_restaurant_button = br.find_element_by_css_selector(restaurant_url_sel)
print str(ta_restaurant_button.text)
elapsed_time = time.time() - start_time
print("---|||||||||||||||||||||||||| %s seconds |||||||||||||||||||||||||||||||---" % elapsed_time)
print br.page_source
url = 'http://www.tripadvisor.com/Tourism-g150812-Playa_del_Carmen_Yucatan_Peninsula-Vacations.html'
test(url)
追溯:
Traceback (most recent call last):
File "scrapeall_destination.py", line 20, in <module>
get_restaurants(url)
File "/Users/pablocastelo/Desktop/tripadvisor/get_restaurants.py", line 67, in get_restaurants
ta_restaurant_button.click()
File "/Library/Python/2.7/site-packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/remote/webelement.py", line 65, in click
self._execute(Command.CLICK_ELEMENT)
File "/Library/Python/2.7/site-packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/remote/webelement.py", line 385, in _execute
return self._parent.execute(command, params)
File "/Library/Python/2.7/site-packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 173, in execute
self.error_handler.check_response(response)
File "/Library/Python/2.7/site-packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: {"errorMessage":"Element is not currently visible and may not be manipulated",
您可以看到的输出(我只包含页面源的相关部分)确实显示元素在那里,发生了什么?测试在Ubuntu上运行没有问题。
MacbookAir:~ hotr$ python ~/desktop/test2.py
---|||||||||||||||||||||||||| 11.2492170334 seconds |||||||||||||||||||||||||||||||---
<li class="restaurants twoLines">
<a href="/Restaurants-g150812-Playa_del_Carmen_Yucatan_Peninsula.html" data-trk="restaurants_nav" onmousedown="ta.common.header.addClearParam(this);"> <img src="/img2/x.gif" class="typeIcon sprite-restaurants_icon" alt="" width="42" height="41">
<span class="typeName">Restaurants</span> <span class="typeQty">(745)</span>
<span class="contentCount">46,865 Reviews</span>
<img src="/img2/x.gif" class="sprite-nav_arrow" width="8" height="11">
</a> </li>
答案 0 :(得分:0)
Wait表示元素 to be visible :
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test(urlx):
br = webdriver.PhantomJS('phantomjs')
br.get(urlx)
ta_restaurant_button = WebDriverWait(br, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, '.restaurants a'))
)
url = 'http://www.tripadvisor.com/Tourism-g150812-Playa_del_Carmen_Yucatan_Peninsula-Vacations.html'
test(url)
答案 1 :(得分:0)
你可以试试这个:
browser = webdriver.PhantomJS()
browser.set_window_size(1124, 850) # set browser size.
browser.get("http\:example.com") # Load page
解决方案是我们需要在执行browser.get(&#34;&#34;)之前设置虚假的浏览器大小。 请参阅github
中的问题