user3157427
user3157427

Reputation: 63

How to click to specific coordinate of a page

I need to click a link in a page with Selenium-webdriver , but i can't access the element using the value of href like below:

var link = WebDriver.FindElements(By.XPath("//a[@href='example.html']"));

because elements in the page is in a frame and in the source of another page. So I need to click to a specific coordinate where the link is. How can I make it by using selenium webdriver.

Upvotes: 1

Views: 4242

Answers (2)

Paul Harris
Paul Harris

Reputation: 5819

It sounds like what you need to be able to do is switch to a different frame then look in that frame for the element you want rather than using co-ordinates. the problem with co-ordinates is that if you run it on a different machine the co-ordinates could be different.

IWebElement frame = driver.FindElement(By.CssSelector("xxx"));
driver.SwitchTo().Frame(frame); 

then search for your element as normal. then once you are done call

driver.SwitchTo().DefaultContent();

and you will be back to the original page.

Upvotes: 4

Petr Mensik
Petr Mensik

Reputation: 27506

You can do that through the Actions class.

Actions builder = new Actions(driver);       
builder.MoveByOffset(x, y).Click().Perform();

Or alternatively via JavascriptExecutor, just pass to it JS code needed to move to the link and click it.

Upvotes: 2

Related Questions