[英]Python Selenium: Can't find an element on page
我对在硒中使用python很陌生。
我一直在尝试选择网页上的按钮。 这是检查按钮元素之后出现的HTML片段:
<a class="btn col-xs-3 nav-btns" ui-sref="salt.dashboard.reports.minions" href="/dashboard/reports/minions/">
<span class="ssIcons-icon_reports salt-icon-3x ng-scope active" bs-tooltip="" data-title="Reports" container="body" placement="bottom" animation="none" data-trigger="hover" ng-class="{'active': state.current.name =='salt.dashboard.reports' … || state.current.name =='salt.dashboard.reports.minions'}">
::before
</span>
</a>
我已经尝试了所有我能想到的。 这是我尝试过的一些方法:
element = driver.find_element_by_class_name("btncol-xs-3")
element = driver.find_element_by_name("Reports")
element = driver.find_element_by_id("Reports")
我不断得到的错误是:
selenium.common.exceptions.NoSuchElementException:消息:无法找到元素:{“方法”:“类名”,“选择器”:“ salt.dashboard.reports”} Stacktrace:在FirefoxDriver.prototype.findElementInternal_(file:// /tmp/tmpoRPJXA/extensions/fxdriver@googlecode.com/components/driver-component.js:10299),位于FirefoxDriver.prototype.findElement(file:///tmp/tmpoRPJXA/extensions/fxdriver@googlecode.com/components/driver -component.js:10308),位于DelayedCommand.prototype.executeInternal_( file:///tmp/tmpoRPJXA/extensions/fxdriver@googlecode.com/components/command-processor.js:12287),位于DelayedCommand.prototype.execute / <(file:/// tmp / tmpoRPJXA / extensions / fxdriver @ googlecode .com / components / command-processor.js:12229)root @ chris-salt:/ home / chris / Documents / projects / python-selenium#
通过data-title
查找元素:
driver.find_element_by_css_selector("span[data-title=Reports]")
或者,如果你需要去的a
标签:
driver.find_element_by_xpath("//a[span/@data-title = 'Reports']")
克里斯,
您粘贴的跨度没有名为id的属性。
另外,您的类选择器太宽,我建议在dom结构后面使用更明确的路径。 切记,可能有多个具有该类名称的元素。
另外,您尝试通过属性名称查找该元素中没有的属性名称。
最后,看来您可能正在使用角度。 您要查找的输入是否使用javascript动态创建?
而且,为什么要使用root进行此测试?
在进行断言之前,您可以存储生成的html并手动检查您是否确实具有该元素吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.