C#: Selenium Selectors

Drupal8 Logout Link at footer ( XPath vs LinkText )

  • Had MUCH difficulty hooking into the logout link using XPATH for
  • JS window.scrollTo
  • By.LinkText(“Log out”) seems to be more accessible
  • using By.LinkText allowed Selenium to natively 'scroll to' on the 'Click()' method

Selenium basic selectors

XPath

el = driver.FindElement(By.XPath("//a[@href='/uc/user/logout']"));

Id

el = driver.FindElement(By.Id("username"));

LinkText

el = driver.FindElement(By.LinkText("Log out"));

JS scrollIntoView

  • when element found by XPath(//a), this didn't work
el = driver.FindElement(By.LinkText("Log out"));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", el);

ILocatable & Point

  • when element found by XPath(//a), this didn't work
  • this technique assigns an 'Locatable object casting from selenium webdriver
  • then cast the Ilocatable to a Point
  • then create a string using the Point.X & Point.Y coords
  • then pass the string to js window.scrollTo
el = driver.FindElement(By.LinkText("Log out"));
ILocatable ell = (ILocatable)driver.FindElement(By.LinkText("Log out"));
Point p = el.Location;
string jss = String.Format("window.scrollTo({0},{1})", p.X, p.Y);
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript(jss);

Actions – MoveToElement(el)

  • when element found by XPath(//a), this didn't work
  • This is a MOUSE move, not a scroll to
el = driver.FindElement(By.LinkText("Log out"));
Actions act = new Actions(driver);
act.MoveToElement(el);
act.Perform();
return el;


 

Tags