selenium+PhantomJS 报错

廰雨 发布于 2018/11/22 16:28
阅读 25
收藏 0

最近用selenium出现了好几次这个错误,不知道怎么解决,求大神帮忙看看。

报错信息:

Traceback (most recent call last):

  File "<ipython-input-1-85def73aaa81>", line 1, in <module>
    runfile('D:/Code/Crawler/tiyu/FootballTeam.py', wdir='D:/Code/Crawler/tiyu')

  File "D:\EnvironMent\Anaconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "D:\EnvironMent\Anaconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/Code/Crawler/tiyu/FootballTeam.py", line 32, in <module>
    name=aaa.find_element_by_xpath('./table/tbody/tr[4]/td/a').text

  File "D:\EnvironMent\Anaconda\lib\site-packages\selenium\webdriver\remote\webelement.py", line 76, in text
    return self._execute(Command.GET_ELEMENT_TEXT)['value']

  File "D:\EnvironMent\Anaconda\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)

  File "D:\EnvironMent\Anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)

  File "D:\EnvironMent\Anaconda\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)

WebDriverException: {"errorMessage":"undefined is not an object (evaluating '(y(a)?y(a).parentWindow||y(a).defaultView:window).getComputedStyle(a,null).MozTransform.match')","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:58163","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"GET","url":"/text","urlParsed":{"anchor":"","query":"","file":"text","directory":"/","path":"/text","relative":"/text","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/text","queryKey":{},"chunks":["text"]},"urlOriginal":"/session/4a5777c0-ee2f-11e8-aba5-b93db85d4737/element/:wdc:1542874752402/text"}}

源码如下:

url='http://zq.win007.com/cn/TeamHeadPage/36.html'

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5")
browser= webdriver.PhantomJS(desired_capabilities=dcap)
browser.maximize_window()   
browser.set_page_load_timeout(40)
browser.get(url)

main_div=browser.find_elements_by_xpath('//div[@id="MainDiv"]/table/tbody/tr/td')
print(len(main_div))
aaa=main_div[0]
name=aaa.find_element_by_xpath('./table/tbody/tr[4]/td/a')

for mian in main_div:
    try:
        name=mian.find_element_by_xpath('./table/tbody/tr[4]/td/a')
        print(name.text)
        print(name.get_attribute('href'))
    except:
        print('the td is empty')
        continue

加载中
返回顶部
顶部