简体   繁体   中英

Can't find CssSelector with Selenium

I am trying to do some automated tests with Selenium, but I have a problem. I want to be able to press a special button, but this button does not have an ID or Class , which is the first thing I try to look for.

I Think that I need to use the CssSelector , but how exactly should I write to find the correct button in my test?

driver.FindElement(By.CssSelector(??))

在此处输入图片说明

You can use the sibling <table> id as starting point

driver.FindElement(By.CssSelector("#tbl_bestallningslita ~ button"));

Or use the onclick attribute (partial)

driver.FindElement(By.CssSelector("[onclick*='btn_taBort']"));

Any of the below CSSLocators should help:

button[accessKey="T"]

OR

button[onclick="folkbokforing_form.btn_taBort.click()"]

You can use xpath too, xpath gives you more flexibility over cssselector although its quite slower than cssselector.

driver.findElement(By.XPath("*//button[@onclick='folkbokforging_form.btn.taBort.click()']"));

or use combined two attributed xpath,

driver.findElement(By.XPath("*//button[@accesskey='T' and contains(@onlcinck,'btn_taBort.click()')]");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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