简体   繁体   English

无法<a>使用 Selenium 定位元素</a>

[英]Unable to locate <a> element using Selenium

I'm a newbie with Selenium trying a project out and I'm trying to click on a link on a page but Selenium says it could not find the element.我是 Selenium 的新手,正在尝试一个项目,我正在尝试单击页面上的链接,但 Selenium 说它找不到该元素。 Here is the inspected element:这是检查的元素:

<a data-action="renditions--schedule-item-list#showAvailableItems" class="available-items-btn">
        <i class="icon-bullhorn"></i>
        Available Items
</a>

Here is what I've tried:这是我尝试过的:

driver.find_element_by_xpath('//a[text()="Available Items"]').click()

and

driver.find_element_by_class_name("a.available-items-btn").click()

Any idea what's wrong?知道有什么问题吗?

Sometimes finding by exact text match doesn't always work.有时通过精确的文本匹配查找并不总是有效。 Try this:尝试这个:

driver.find_element_by_xpath("//a[contains(text(), 'Available Items')]")

Or, try your second attempt again but remove the a.或者,再次尝试第二次尝试,但删除a.

driver.find_element_by_class_name("available-items-btn").click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM