Python mac上的Selenium提供elementNotVisibleException
以下是测试用例:Python mac上的Selenium提供elementNotVisibleException,python,macos,selenium,selenium-webdriver,phantomjs,Python,Macos,Selenium,Selenium Webdriver,Phantomjs,以下是测试用例: #!/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() b
#!/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",
回溯(最近一次呼叫最后一次):
文件“scrapeall_destination.py”,第20行,在
get_餐厅(url)
文件“/Users/pablocastelo/Desktop/tripadvisor/get_restaurants.py”,第67行,在get_restaurants中
ta_餐厅按钮。单击()
文件“/Library/Python/2.7/site packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/remote/webelement.py”,第65行,单击
self.\u执行(命令。单击\u元素)
文件“/Library/Python/2.7/site packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/remote/webelement.py”,第385行,在
返回self.\u parent.execute(命令,参数)
文件“/Library/Python/2.7/site packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/remote/webdriver.py”,执行中的第173行
self.error\u handler.check\u响应(响应)
文件“/Library/Python/2.7/site packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/remote/errorhandler.py”,第166行,在check_响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.ElementNotVisibleException:消息:{“errorMessage”:“元素当前不可见,可能无法操作”,
您可以看到的输出(我只包括页面源代码的相关部分)实际上显示了元素在那里,发生了什么?测试在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>
MacbookAir:~hotr$python~/desktop/test2.py
---||||||||||||||||||||||||||11.2492170334秒|||||||||||||||||||||||||||||||---
对于元素:
您可以尝试以下方法:
browser = webdriver.PhantomJS()
browser.set_window_size(1124, 850) # set browser size.
browser.get("http\:example.com") # Load page
解决方案是,在执行browser.get(“”)之前,我们需要设置一个假浏览器大小。
请参阅中的问题。我在get_restaurants EC.visibility_of_element_(By.CSS_SELECTOR,restaurant_url_Sell))文件“/Library/Python/2.7/site packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/support/wait.py”中的第72行获得异常消息:File“/Users/hotr/Desktop/Desktop/tripadvisor/get_restaurants.py/get_.py”,第71行,在until raise TimeoutException(message)@lawrencesouza中,好的,但是如果你尝试chrome或firefox呢?只是为了测试。是的,它可以与Chromedriver一起工作。但是最好使用phantomjs
browser = webdriver.PhantomJS()
browser.set_window_size(1124, 850) # set browser size.
browser.get("http\:example.com") # Load page