我想去一个网站。在那里搜索一个关键字,并在搜索结果中找到HP产品的一些细节。这是我的代码,没有错误但也没有输出。有时我也会得到StaleElementException。
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome( "C:\All\chromedriver_win32\chromedriver.exe" )
driver.get('https://www.insight.com/en_US/search.html')
driver.find_element_by_id("search-tab-banner-box-input").send_keys("printer")
wait( driver, 10 ).until( EC.visibility_of_element_located( (By.ID, "search-tab-banner-box-input") ) ).send_keys(Keys.ENTER )
wait( driver, 10 ).until( EC.visibility_of_element_located( (By.ID, "search-tab-banner-box-input") ) ).send_keys(Keys.ESCAPE )
items_count = 0
while True:
try:
items = driver.find_elements_by_class_name("prod-section-container")
for index, item in enumerate( items ):
if 'HP'in item.text:
print(index, item.text, item.find_element_by_class_name('prod-part-number show-for-medium-up'))
items_count += len( items )
except:
break
try:
driver.find_element_by_xpath( '//div[@class="next-btn"]/a' ).click()
except:
break
谁能告诉我哪里出错?
答案 0 :(得分:1)
find_element_by_class_name('prod-part-number')
(而不是find_element_by_class_name('prod-part-number show-for-medium-up')
)。
>>> item = items[0]
>>> item
<selenium.webdriver.remote.webelement.WebElement (session="f81b46ea9715f98e1eaf6e35ae62f601", element="0.9138180044548743-2")>
>>> item.text
'HP LaserJet Enterprise M506n - printer - monochrome - laser\nInsight Part # : F2A68A#BGJ\nMfr Part #: F2A68A#BGJ\n1 year warranty\nAdd to My Compare List\nCompare Similar\nList priceUSD $649.99\n3,710 in stock\nAdd to Cart'
>>>
>>> item.find_element_by_class_name('prod-part-number show-for-medium-up')
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 291, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 511, in find_element
{"using": by, "value": value})['value']
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 494, in _execute
return self._parent.execute(command, params)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class names not permitted
(Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 10.0.15063 x86_64)
>>> item.find_element_by_class_name('prod-part-number')
<selenium.webdriver.remote.webelement.WebElement (session="f81b46ea9715f98e1eaf6e35ae62f601", element="0.9138180044548743-12")>
>>> item.find_element_by_class_name('prod-part-number').text
'Insight Part # : F2A68A#BGJ\nMfr Part #: F2A68A#BGJ'