我试图打印标题,为编程点击的链接。我遇到了一些麻烦。但是我得到的不是错误,而是其他的东西。是的,我尝试了"text"但实际上却给了我一个错误
Main.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import unittest
class Test():
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get("https://orlando.craigslist.org/search/apa?s=100")
def task(self):
driver = self.driver
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]
ln.click()
print (ln)
back = driver.find_element_by_xpath('/html/body/section/header/nav/ul/li[3]/p/a').click()
WebDriverWait(driver,3)
if i == 5:
break
def getInfo(self,ln):
driver = self.driver
if ln:
print (ln)
else:
print ("No listing name")
def initialize():
return Test
def teardown(self):
self.driver.quit()
run = Test()
run.setUp()
tas = run.task()
run.teardown()
WebDriverWait(tas,3)
if __name__ == '__main__':
unittest.main()
这是我得到的
<selenium.webdriver.remote.webelement.WebElement (session="214c8aa4e4a919e11149b48fc1971ebf", element="0.47651721823993864-1")>
<selenium.webdriver.remote.webelement.WebElement (session="214c8aa4e4a919e11149b48fc1971ebf", element="0.8722193647053464-2")>
<selenium.webdriver.remote.webelement.WebElement (session="214c8aa4e4a919e11149b48fc1971ebf", element="0.7501031429429339-3")>
<selenium.webdriver.remote.webelement.WebElement (session="214c8aa4e4a919e11149b48fc1971ebf", element="0.1697348713718274-4")>
<selenium.webdriver.remote.webelement.WebElement (session="214c8aa4e4a919e11149b48fc1971ebf", element="0.8560861666106063-5")>
<selenium.webdriver.remote.webelement.WebElement (session="214c8aa4e4a919e11149b48fc1971ebf", element="0.8604981647081367-6")>
这是print(ln.text)给我的东西
Traceback (most recent call last):
File "main.py", line 45, in <module>
tas = run.task()
File "main.py", line 20, in task
print (ln.text)
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)
答案 0 :(得分:3)
您看到的是 public class Bank {
//Declare bankArray as instance variable
ArrayList<Account> bankArray;
public Bank() {
this.bankArray=new ArrayList<>();//use <> operator to make it less verbose
}
//add other code of the class
}
实例表示字符串。相反,您需要通过获取WebElement
属性的值来获取元素的文本:
.text
另请注意,print(ln.text)
行实际上会做什么&#34;没有&#34; (好吧,除了制作一个很快会被垃圾收集的WebDriverWait(driver, 3)
的实例),如果你想要一个硬编码的时间延迟,使用WebDriverWait
,虽然更好的想法是明确地等待某个条件通过WebDriverWait
and .until()
来满足。