python selenium xpath/css 选择器 [英] python selenium xpath/css selector
本文介绍了python selenium xpath/css 选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 xpath 或 css 选择器选择特定信息.但我不断收到错误消息.有人可以帮忙看看这里出了什么问题吗?
I am trying to select for a particular information using xpath or css selector. But I keep on getting error messages. Can someone help with seeing what's wrong here?
这是我代码的一部分
output = driver.find_element_by_xpath("//td[@class_= 'sku']")
print(output)
<小时>
使用下面给出的修改,我收到此错误消息:
Using the modifications give below, I am obtaining this error message:
Traceback (most recent call last):
File "sa.py", line 25, in <module>
output = driver.find_element_by_xpath("//td[@cl
File "C:\Python27\lib\site-packages\selenium\webd
return self.find_element(by=By.XPATH, value=xpa
File "C:\Python27\lib\site-packages\selenium\webd
{'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium\webd
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webd
raise exception_class(message, screen, stacktra
selenium.common.exceptions.NoSuchElementException:
td[@class=\'sku\']/p"}' ; Stacktrace:
at FirefoxDriver.prototype.findElementInternal_
ver@googlecode.com/components/driver_component.js:9
at FirefoxDriver.prototype.findElement (file://
ecode.com/components/driver_component.js:9479)
at DelayedCommand.prototype.executeInternal_/h
er@googlecode.com/components/command_processor.js:1
at DelayedCommand.prototype.executeInternal_ (f
@googlecode.com/components/command_processor.js:114
at DelayedCommand.prototype.execute/< (file:///
code.com/components/command_processor.js:11402)
<小时>
这是我写的代码:
This is the code that I have written:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = "http://www.sigmaaldrich.com/united-states.html"
#cas = "1300746-79-5"
cas = "100-44-7"
driver = webdriver.Firefox()
driver.get(url)
inputElement = driver.find_element_by_name("Query")
inputElement.send_keys(cas)
inputElement.submit()
pricing_link = driver.find_element_by_css_selector("li.priceValue a")
pricing_link.click()
output = driver.find_element_by_xpath("//td[@class='sku']/p")
print(output)
driver.quit()
推荐答案
和OP和alecxe讨论后,这是一个时间问题,需要使用WebDriverWait 等待表加载.
After the discussion with OP and alecxe, this is a timing issue, where you need to use WebDriverWait to wait for table loading.
# XPath to select <p> inside <td> with class name 'sku'
outputs_by_xpath = WebDriverWait(driver, 10).until(
lambda driver : driver.find_elements_by_xpath(".//td[@class='sku']/p")
)
# or CSS selector
outputs_by_css = WebDriverWait(driver, 10).until(
lambda driver : driver.find_elements_by_css_selector("td.sku > p")
)
for output in outputs_by_xpath:
print(output.text)
print("\n")
for output in outputs_by_css:
print(output.text)
输出:
185558-50G
185558-250G
185558-1KG
185558-2KG
185558-50G
185558-250G
185558-1KG
185558-2KG
这篇关于python selenium xpath/css 选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文