【问题标题】:Selenium basic opening a urlSelenium 基本打开一个 url
【发布时间】:2015-10-14 08:54:42
【问题描述】:

我只是从硒开始。

我做了一个简单的 python 脚本,它应该打开一个 url 并在那里打印产品的价格。

这里是:

from selenium import webdriver
import time
driver = webdriver.PhantomJS(executable_path='/usr/bin/phantomjs')
url = 'http://www.stance.com/shop/product/paint-trap'
print "Driver Made"
driver.get(url)
print "URL got"
price = driver.find_element_by_xpath('//*[@id="h1--title-price"]/span[2]').text
print price
driver.close()

但是,它只打印:“Driver Made”,从不打印“URL got”和价格。

似乎卡在了 driver.get(url) 上,但我不知道为什么。

我想知道如何打印价格以及如何阻止 driver.get(url) 永远运行。

如果我用 ctrl C 打断它,我会得到:

Driver Made
^CTraceback (most recent call last):
  File "test.py", line 6, in <module>
    driver.get(url)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 213, in get
    self.execute(Command.GET, {'url': url})
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 199, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 395, in execute
    return self._request(command_info[0], url, body=data)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 463, in _request
    resp = opener.open(request, timeout=self._timeout)
  File "/usr/local/lib/python2.7/urllib2.py", line 404, in open
    response = self._open(req, data)
  File "/usr/local/lib/python2.7/urllib2.py", line 422, in _open
    '_open', req)
  File "/usr/local/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python2.7/urllib2.py", line 1214, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/local/lib/python2.7/urllib2.py", line 1187, in do_open
    r = h.getresponse(buffering=True)
  File "/usr/local/lib/python2.7/httplib.py", line 1045, in getresponse
    response.begin()
  File "/usr/local/lib/python2.7/httplib.py", line 409, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python2.7/httplib.py", line 365, in _read_status
    line = self.fp.readline(_MAXLINE + 1)
  File "/usr/local/lib/python2.7/socket.py", line 476, in readline
    data = self._sock.recv(self._rbufsize)
KeyboardInterrupt

【问题讨论】:

  • 你正在打印它 -> 7 行 -> 打印“URL got”
  • "URL got" 永远不会打印,因为 get(url) 永远运行。我想知道为什么 get(url) 永远运行以及如何停止它。 @ShubhamJain
  • $14.00 是我得到的结果。我猜代码工作正常
  • 奇怪,感谢运行它@vks 你也在使用 phantomjs 吗?
  • @Rorschach no firefox .... 检查是否支持phantom

标签: python selenium selenium-webdriver


【解决方案1】:

工作代码打印-(在 Windows 中)。

Driver Made
URL got
$14.00

工作代码如下

from selenium import webdriver
import time
driver = webdriver.PhantomJS(executable_path=r"C:\Users\Desktop\phantomjs.exe")

driver.set_window_size(1120, 550)
url = 'http://www.stance.com/shop/product/paint-trap'
print "Driver Made"
driver.get(url)
print "URL got"
driver.implicitly_wait(5)
price = driver.find_elements_by_xpath("(//*[@id='h1--title-price']/span)[2]")
for i in price:
    print i.text
driver.close()

注意确保幻象可执行路径和硒库在其他操作系统中是正确的。

【讨论】:

  • 可悲的是,我的仍然永远运行这个。一定是一些与代码无关的错误,我将尝试重新安装 phantomJS,如果不起作用,我将尝试 firefox
  • 感谢您确认代码有效,我会将您的答案标记为正确,因为代码有效,这就是我所要求的。我认为我的 ubuntu 运行异常缓慢。所以,我会着手解决这个问题。
猜你喜欢
  • 1970-01-01
  • 2011-11-12
  • 1970-01-01
  • 2016-09-26
  • 2020-09-01
  • 2013-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多