Reputation: 183
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>
</div>
I am trying to access <a>
with Selenium
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?
Upvotes: 1
Views: 1956
Reputation: 183
That's how I found the answer because div element was enclosed in a frame and as per input from @saurabhgaur:
driver.Navigate().GoToUrl("https://websunp8.bk.datev.de/zws/ShowMenu.do");
driver.SwitchTo().Frame("content");
driver.FindElement(By.XPath("//a[@class = 'buttonLinkText' and text() = 'Hilfe']")).Click();
Upvotes: 0
Reputation: 23805
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 :-
driver.FindElement(By.LinkText("Hilfe"));
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
driver.SwitchTo().DefaultContent();
Upvotes: 2