Shakti saxena Shakti saxena - 1 year ago 83
CSS Question

how to select anchor with class name in it

I am trying to automate testing for my webpage. on webpage, I need to select an anchor with a class name and text. the anchor is enclosed in a div element.

<div class="margins0300">
<a tabindex="-32768" class="buttonLinkText">Hilfe</a>

I am trying to access
internet explorer webdriver but not able to. here is my code:

driver.FindElement(By.XPath("//a[contains(@class,'buttonLinkText') and .//text()='Hilfe']"));

but when I execute it, no element is found.

I would really appreciate if someone can help me with it?

Answer Source

Actually you're passing syntax as xpath while try to find element using By.CssSelector() which is wrong. You should try using By.Xpath() as below :-

driver.FindElement(By.Xpath("//a[@class = 'buttonLinkText' and text() = 'Hilfe']"));

You can also find this element using By.LinkText() as below :-


Edited :- If you're still unable to find this element try using WebDriverWait to wait until element exists as below :-

IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3))
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Xpath("//a[@class = 'buttonLinkText' and text() = 'Hilfe']")));

Note :- Make sure this element is not inside any frame/iframe. If it is you need to switch that frame/iframe before finding element as :-

driver.SwitchTo().Frame("frame/iframe name or id");

//Now find the element using above code

//After doing all stuff inside frame/iframe you need to switch back to default content 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download