0

我只是从这里运行 selenium 的示例代码:

http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html

代码是:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()

但这对我不起作用,这是它的响应:

 Traceback (most recent call last):
  File "test.py", line 4, in <module>
    driver = webdriver.Firefox()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 62, in __init__
    desired_capabilities=capabilities)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 72, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 114, in start_session
    'desiredCapabilities': desired_capabilities,
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 165, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 136, in check_response
    raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message:
...
<div id="content">
<p>The following error was encountered while trying to retrieve the URL: <a href="http://127.0.0.1:60106/hub/session">http://127.0.0.1:60106/hub/session</a></p>

<blockquote id="error">
<p><b>Connection to 127.0.0.1 failed.</b></p>
</blockquote>

<p id="sysmsg">The system returned: <i>(111) Connection refused</i></p>

<p>The remote host or network may be down. Please try the request again.</p>
...
4

2 回答 2

0

当 Selenium 启动 Firefox 时

browser = webdriver.Firefox()

它访问的第一个地址是 localhost - 127.0.0.1:xxxxx

如果您使用的是代理服务器,则无法使用代理集访问本地主机。因此,首先您需要使用以下命令关闭代理:

unset http_proxy
unset ftp_proxy
unset socks_proxy
unset https_proxy

关闭代理后,您的 Firefox 应该可以正常启动。但是现在您需要在 Firefox 上设置您的代理。此处描述的技术有效。

于 2013-11-01T16:24:26.770 回答
0

您没有运行完整的示例。您发布的链接包含以下代码:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()

这很好用。您问题中代码的编辑版本缺少某些部分,因此失败。具体来说,您缺少以下两行:

elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)

这将启动雅虎搜索“seleniumhq”。该搜索的结果是您要在其中找到元素的内容。

如果您不进行搜索,它将失败:

browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
于 2013-08-12T13:21:26.607 回答