类型错误:+:' WebElement'不支持的操作数类型和' WebElement'对于硒

时间:2017-03-19 00:59:00

标签: python selenium

我试图导出一个excel电子表格,它给我带来了问题,我试图做的是将信息存储在spreadsheet中。有什么建议 ? 我在这里引用了一些资源,就像这个类似的question

Main.py

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import csv
from selenium.webdriver.support.ui import WebDriverWait
import unittest





class Test():
    # filename = "list.csv"
    # f = open(filename,"w")
    # headers = "listing name, aval \n"

    # f.write(headers)

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://orlando.craigslist.org/search/apa?s=100")
        self.filename = "list.csv"
        self.f = open(self.filename,"w")
        self.headers = "listing name, aval \n"

    def task(self):
        driver = self.driver
        filename = self.filename
        f = self.f
        headers = self.headers
        ln = driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a')
        size = len(driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a'))
        for i in range(0, size):
            ln = driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a')
            ln = ln[i]
            self.getLn(ln)
            ln.click()
            av = driver.find_element_by_xpath('/html/body/section/section/section/div[1]/p[1]/span[3]')
            self.getAv(av)
            back = driver.find_element_by_xpath('/html/body/section/header/nav/ul/li[3]/p/a').click()
            WebDriverWait(driver,3)
            f.write(ln + av + "\n")
            if i == 5:
                f.close()
                break





    def getLn(self,ln):
        driver = self.driver
        ln = ln
        if ln:
            print (ln.text)
        else:
            print ("No listing name")



    def getAv(self,av):
        driver = self.driver
        if av:
            print (av.text)
        else:
            print ("No listing name")


    def initialize():
        return Test

    def teardown(self):
        self.driver.quit()



run = Test()
run.setUp()
tas = run.task()
run.teardown()






if __name__ == '__main__':
    unittest.main()

回溯错误:

  File "main.py", line 41, in task
    f.write(ln + av + "\n")
TypeError: unsupported operand type(s) for +: 'WebElement' and 'WebElement'

编辑,错误

Traceback (most recent call last):
  File "main.py", line 77, in <module>
    tas = run.task()
  File "main.py", line 41, in task
    f.write(ln.text + av.text + "\n")
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 73, in text
    return self._execute(Command.GET_ELEMENT_TEXT)['value']
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 491, in _execute
    return self._parent.execute(command, params)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 238, in execute
    self.error_handler.check_response(response)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=56.0.2924.87)
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Mac OS X 10.11.2 x86_64)

2 个答案:

答案 0 :(得分:1)

您收到该错误是因为lnav是WebElements,而您无法添加它们。

def task(self):
        driver = self.driver
        filename = self.filename
        f = self.f
        headers = self.headers
        ln = driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a')
        size = len(driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a'))
        for i in range(0, size):
            ln = driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a')
            ln = ln[i]
            self.getLn(ln)
            ln.click()
            av = driver.find_element_by_xpath('/html/body/section/section/section/div[1]/p[1]/span[3]')
            self.getAv(av)
            back = driver.find_element_by_xpath('/html/body/section/header/nav/ul/li[3]/p/a').click()
            WebDriverWait(driver,3)
            f.write(ln + av + "\n") ### Adding WebElement's isn't allowed
                f.close()
                break

也许你的意图是连接(加入)页面上元素的文本。在这种情况下,请将f.write(ln + av + "\n")更改为f.write(ln.text + av.text + "\n")

我希望这能回答你的问题。快乐的编码!

答案 1 :(得分:0)

av和ln是WebElement,因此无法连接它们。 我想要你想要WebElement中的文本,所以你需要像这样访问它:

f.write(ln.text + av.text + "\n")