简体   繁体   中英

How to get the last div element in c #?

There is such a piece of html.

在此处输入图片说明

I need to get text from the last div element. For example, at the moment I get the ninth element like this:

var textmsg = webDriver.FindElement(By.XPath("//*[@id='main']/div[3]/div/div/div[2]/div[9]/div/div/div/div[1]/div/span[1]/span")).Text;
            string textmsgS = Convert.ToString(textmsg);
            Console.WriteLine(textmsgS);

But as I said, I need to get exactly the last element. Thanks!

try like this,i hope, by this way your issue will fix

driver.findElement(By.xpath("//*[@id='main']/div[last()]")).click();

or by using css selector you can try like this

You can have an array list of elements and then select the last element by:

List<WebElement> elements= driver.findElements(By.css("css"));

 element = elements.get(list.size() - 1);

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