按标题查找并单击元素 Python Selenium
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25363966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find and click element by title Python Selenium
提问by cask
I am looking site. In inspect element see this:
我正在寻找网站。在检查元素看到这个:
<span id="item60" title="Havai 30" class="item button link">Get</span>
<span id="item90" title="Classic 50" class="item button link">Get</span>
Need to get and click element by title. Something like this:
需要按标题获取和单击元素。像这样的东西:
browser.find_element_by_xpath('//*[@id="item60"]').click()
But via title.
但是通过标题。
采纳答案by cask
Like barak manos said the answer was:
就像 barak manos 所说,答案是:
'//*[@title="Havai 30"]'
With [0] at ending, case it was list.
以 [0] 结尾,情况是列表。
回答by Simplans
browser.find_element_by_xpath('//*[@title="Havai 30"]').click()
This Will Work for me Like you said.
就像你说的那样,这对我有用。
回答by Krzysztof Walczewski
For me works with Page Object Pattern:
对我来说,使用页面对象模式:
@FindBy(xpath = "//*[@title='Havai 30']")
WebElement imHavai;
回答by Master Yi
For java if someone was looking for the answer here like me:
对于java,如果有人像我一样在这里寻找答案:
String title="SOME TITLE";
driver.findElement(By.cssSelector("[title^='"+title+"']")).click();

