简体   繁体   中英

Java Selenium FindBy cannot find div by unique class name

I am using Selenium with Java and I have encountered issue that I am unable to find div by class name, even though it is unique:

<div class="123123-randomclassname"></div>

I am able to find any other element, eg input, button, etc. I have issues with div tag only.

I have tried getting this web element using either @FindBy() annotation and findElement() method:

driver.findElement(By.className("123123-randomclassname")) driver.findElement(By.cssSelector("div[class='123123-randomclassname'"))

@FindBy(className = "123123-randomclassname") @FindBy(css = "div[class='123123-randomclassname'" )

Any of these solutions did not work and I couldn't find element.

Try with following css selector

driver.findElement(By.cssSelector("div[class^='123123-']"))

//@ and ]closing bracket was missing in syntax
WebElement DivTag = driver.findElement(By.xpath("div[@class='123123-randomclassname']")); //OR
@FindBy(xpath = "div[@class='123123-randomclassname']") WebElement DivTag2;

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