Selenium how to select an object by class

限于喜欢 提交于 2019-12-03 15:33:50

问题


I have a web page with a form and has a field that uses the jquery autocomplete function.

This is how the HTML renders after a user name returns 1 or more results.

However I cannot figure out how to make Selenium "click" a result.

Can I do a jQuery type of selector. e.g.

$(".ul.ui-autocomplete li:first a")

回答1:


Use XPath selector in Selenium:

xpath=//li[contains(@class, 'ui-autocomplete')]/li[1]/a

not checked, might require some corrections.




回答2:


in response to "Can I do a jQuery type of selector," jQuery uses CSS selectors. Selenium can also use CSS selectors; just prefix the selector with "css=". so:

css=.ul.ui-autocomplete li:first a



回答3:


Next way to use xpath like this

xpath=/html/body/ul[2]/li[1]/a



回答4:


Suppose you have a dynamic XPATH then you can point to an element like this

driver.findElement(By.className(""));



来源:https://stackoverflow.com/questions/5543724/selenium-how-to-select-an-object-by-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!