1

I have a ton of tests written in xpath that I need to convert to css.

I don't want to use an automation tool as I want to take the opportunity to learn the way to do things using the css approach in selenium.

Where can I find examples of 'before' and 'after' when changing from xpath to css for locators

4

2 回答 2

3

我知道你自己已经回答了,但我发誓这个网站,有一个非常漂亮的图表,可以准确地显示你在问什么:

https://www.simple-talk.com/dotnet/.net-framework/xpath,-css,-dom-and-selenium-the-rosetta-stone/

于 2013-05-03T08:21:37.770 回答
1

以下是 30 个“之前”和“之后”xpath 到 css 的示例:

请注意,css 也使用基于 0 的定位,而 xpath 使用基于 1 的定位,如下所示:

例子:

xpath=(//li[contains(@id,'topic_roles_input')]//input[@type="checkbox"])[1]
css=li#topic_roles_input input[type=checkbox]:nth(0)

名单:

//ul[contains(@id,'district-switcher')]//li/ul/li[4]/a
css=ul#district-switcher li > ul > li:nth(3) > a

//ul[contains(@id,'district-switcher')]//a[contains(text(),'District Management Council')]
css=ul#district-switcher a:contains('District Management Council')

//ul[contains(@id,'district-switcher')]//a[contains(text(),'${QA_run_number}')]
css=ul#district-switcher a:contains('${QA_run_number}')

//h3[contains(@id,'active-district')]
css=h3#active-district

//ul[contains(@class,'home-menu')]//a[contains(text(),'Calendar')]
css=ul.home-menu a:contains("Calendar")

//tr[1]//td[contains(@class,'starts_on')]
css=tr td.starts_on

//tr[1]//td[contains(@class,'ends_on')]
css=tr td.ends_on

//ul[contains(@class,'home-menu')]//a[contains(text(),'Schools')]
css=ul.home-menu a:contains('Schools')

//table[contains(@id, 'schools')]//tbody//tr//td/a
css=table#schools tbody tr td a

//a[contains(text(),'6DAYERS')]
css=a:contains('6DAYERS')

//ul[contains(@class,'home-menu')]//a[contains(text(),'Students')]
css=ul.home-menu a:contains(Students)

//body//td[contains(text(),'QA-001')]
css=td:contains('QA-001')

//ul[contains(@class,'home-menu')]//a[contains(text(),'Roles')]
css=ul.home-menu a:contains(Roles)

//table//tr//td[contains(text(),"Language Therapist")]
css=table tr td:contains(LanguageTherapist)

//table//tr//td[contains(text(),"Speech Therapist")]
css=table tr td:contains(Speech Therapist )

//table//tr//td[contains(text(),"DELETE_ME")]
css=table tr td:contains(DELETE_ME)

//ul[contains(@class,'home-menu')]//a[contains(text(),'Activities')]
css=ul.home-menu a:contains(activities)

xpath=(//li[contains(@id,'activity_roles_input')]//input[@type="checkbox"][1])
css=li#activity_roles_input input[@type="checkbox"]:nth(0)

//table[contains(@id,'activities')]//tr//td[contains(text(),"Activity001")]
css=table#activities tr td:contains("Activity001")

//ul[contains(@class,'home-menu')]//a[contains(text(),'Practitioners')]
css=ul.home-menu a:contains(Practitioners)

//table//tr//td[contains(text(),'mdurrant+${QA_run_number}_001@dmcouncil.org')]
css=table tr td:contains(mdurrant+${QA_run_number}_001@dmcouncil.org)

//ul[contains(@class,'home-menu')]//a[contains(text(),'Topics')]
css=ul.home-menu a:contains(Topics)

xpath=(//li[contains(@id,'topic_roles_input')]//input[@type="checkbox"])[1]
css=li#topic_roles_input input[type=checkbox]:nth(0)

//a[contains(text(),'Topic001')]
link=Topic001

//ul[contains(@class,'home-menu')]//a[contains(text(),'Settings')]
link=Settings

//ul[contains(@class,'home-menu')]//a[contains(text(),'Settings')]
css=ul.home-menu a:contains(Settings)

xpath=(//li[contains(@id,'setting_roles_input')]//input[@type="checkbox"])[1]
css=li#setting_roles_input input[type=checkbox]:nth(0)

xpath=(//table/tbody/tr//td/a[contains(@class,'delete_link')])
table > tbody > tr td > a.delete_link

//td[contains(text(),'Setting001')]
css=td:contains('setting001')

//a[contains(text(),'Practitioners')]
css=a:contains(Practitioners)

//tr[td[contains(.,'6 Day')]][1]/td[8]/a@href
tr td:contains('6 Day'):nth(0) + td + td a[href]
于 2013-05-02T20:27:26.073 回答