简体   繁体   中英

Selenium: Determine status of WebElement by CssSelector

I am identifying a clickable WebElement by CssSelector . An example code I am using is:

String selectorString = "a.some-text.item";

WebElement we = driver.findElement(By.cssSelector(selectorString));
we.click();

The problem is, that after the item has been clicked its CssSelector changes to a.some-text.item.clicked . I can still securely identify it by using above selectorString variable. However, I only want to click it if it has not been clicked before.

How can I determine the items status, ie whether its cssSelector name is a.some-text.item or a.some-text.item.clicked ?

I only want to click it if it has not been clicked before.

Try using :not(selector) function of cssSelector to determine only unclicked element :-

String selectorString = "a.some-text.item:not(.clicked)";

WebElement we = driver.findElement(By.cssSelector(selectorString));
we.click();

Edited :- If you want to just determine whether element has contains clicked class or not after clicking then try as below :-

String selectorString = "a.some-text.item";
driver.findElement(By.cssSelector(selectorString)).click();

//Now verify element clicked or not 
WebElement we = driver.findElement(By.cssSelector(selectorString));
String msg =  (we.getAttribute("class").contains("clicked")) ? "element clicked" : "element not clicked";
System.out.println(msg);

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