python - Selenium webdriver xpath 过滤

标签 python selenium xpath selenium-webdriver

我的 xpath 语法有什么问题?我试图过滤的节点是,我的代码是:

div class="cmeTableBlockWrapper cmeContentSection cmeContentGroup"
table id="quotesFuturesProductTable1" 
tr[1:]
   td id="quotesFuturesProductTable1_ZCZ4_last"
   td id="quotesFuturesProductTable1_ZCZ4_change"
   td id="quotesFuturesProductTable1_ZCZ4_priorSettle"

url = "http://www.cmegroup.com/trading/agricultural/grain-and-oilseed/corn.html"

driver = webdriver.Chrome() 

driver.get(url) 

table = driver.find_element_by_xpath('//div[class="cmeTableBlockerWrapper cmeContentSection cmeContentGroup"]/table[@id="quotesFuturesProductTable1"]') 

# or table = driver.find_element_by_xpath('//td[contains(div[@class="fixedpage_heading"], "CORN")]/table[@class="homepage_quoteboard"]')

for row in table.find_elements_by_tag_name('tr')[1:]:  
    priorsettle = str(row.find_element_by_id('quotesFuturesProductTable1_ZSX4_priorSettle').text)

最佳答案

查找表格by id ,通过索引获取适当的行(这里最简单的方法):

table = driver.find_element_by_id('quotesFuturesProductTable1')
for row in table.find_elements_by_tag_name('tr')[2:]:
    print row.find_elements_by_tag_name('td')[4].text

打印(先前解决列):

338'2
350'6
359'2
366'2
374'0
384'2
393'6
400'4
404'4
402'0
402'0
418'2
407'4

请注意,我们需要跳过 2 行,而不是一行,因为在实际表格内容开始之前有两个 tr 标记。

关于python - Selenium webdriver xpath 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25937603/

相关文章:

python - 如何修复: RuntimeError: size mismatch in pyTorch

selenium - 使用 selenium Webdriver 如何检查 webelement 是否在屏幕的可见区域之外?

ruby-on-rails - 你如何使用 Minitest、Capybara、Selenium 执行 javascript 测试?

python - 在几次成功的 Selenium Geckodriver 任务后连接被拒绝(操作系统错误 111)(Python)

python - 如何计算 pandas 数据框中的标准差?

python - 尝试在 openSUSE 上安装 pip 时出错

memory-management - libXML:检查节点是否已经取消链接并释放

xml - xsl中字符串的初始数字部分或整数部分

python - 从包含 100,000 个整数的列表中检索两个最高的项目

java - 基于高效 Camel 内容的路由器 : Route XML messages to the correct recipient based on contained tag with Java DSL