Select a random XPath element - c#

Hy,
on a website there are some button:
Previously I copied the old code to the question, this is the actual code:
IWebElement btn_tag_1 = driver.FindElement(By.XPath("//*[#data-testid ='tag_eject']"));
IWebElement btn_tag_2 = driver.FindElement(By.XPath("//*[#data-testid ='tag_close']"));
IWebElement btn_tag_3 = driver.FindElement(By.XPath("//*[#data-testid ='tag_open']"));
IWebElement btn_tag_4 = driver.FindElement(By.XPath("//*[#data-testid ='tag_selected']"));
IWebElement btn_tag_5 = driver.FindElement(By.XPath("//*[#data-testid ='tag_disabled']"));
IWebElement btn_tag_6 = driver.FindElement(By.XPath("//*[#data-testid ='tag_free']"));
In a normal case when I would like to click a selected element for example "btn_tag_1" the code is:
btn_tag_1.Click();
But now I wanted to click one of them selected randomly and I'm totally stucked at this point.
Can You help me in this case?
Thank You!

If you are just looking for some help on how to randomly click one of the buttons then I would do it this way:
using System;
using OpenQA.Selenium;
By[] buttonsBy = {
By.XPath("//*[#data-testid='tag_1']"),
By.XPath("//*[#data-testid='tag_2']"),
By.XPath("//*[#data-testid='tag_3']"),
By.XPath("//*[#data-testid='tag_4']"),
By.XPath("//*[#data-testid='tag_5']"),
By.XPath("//*[#data-testid='tag_6']")
};
int index = new Random().Next(buttonsBy.Length - 1);
IWebElement button = driver.FindElement(buttonsBy[index]);
button.Click();
Create an array with your By selectors, choose a random number within the length of that array, find the element using that random number as the index and then click.
Added after comment with code:
All you need to do is change the string inside each locator to use the code in your comment.
By[] buttonsBy = {
By.XPath("//*[#data-testid='tag_eject']"),
By.XPath("//*[#data-testid='tag_close']"),
By.XPath("//*[#data-testid='tag_open']"),
By.XPath("//*[#data-testid='tag_selected']"),
By.XPath("//*[#data-testid='tag_disabled']"),
By.XPath("//*[#data-testid='tag_free']")
};

Related

Need to enter Text

I tried finding the element using Get but it does not work, thats why i treid with GetElement method
I am trying to enter text in an textbox element found using GetElement in teststack white using C#
i want to know how to cast the automation element to UIitem so that i can do enter() or click operation on that element
var all = appWindow.GetElement(SearchCriteria.ByControlType(ControlType.ComboBox)
.AndByText("Model collapsed"));
var element = all.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.NameProperty, "Edit Box collapsed"));
element.enter("");
when i do element.enter or click it gives error, i think i need to cast it or is there any other way where i can achieve this. Thank you.
After using the below code i was able to enter text.
var all = appWindow.GetElement(SearchCriteria.ByControlType(ControlType.ComboBox)
.AndByText(parentValue));
var element = all.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, childValue));
TextBox textBox = new TextBox(all, appWindow.ActionListener);
TestStack.White.InputDevices.AttachedKeyboard keyboard = appWindow.Keyboard;
textBox .Click();
keyboard.Enter("test");

How can get a href in Selenium Test using c#

I want to select My jobs tag and then on that but there's no Id or Name for given tag just a href but when i am trying to find my cssSelector or by cssClass then it's shows erorr.So any one who can help me
enter image description here
For clicking on My Jobs , you can use this code:
IWebElement element = driver.FindElement(By.LinkText("My Jobs"));
element.Click();
For clicking on New Jobs , you can use this code:
IWebElement element = driver.FindElement(By.LinkText("New Jobs"));
element.Click();
For clicking on My Units , you can use this code:
IWebElement element = driver.FindElement(By.LinkText("My Units"));
element.Click();
For clicking on Timesheets , you can use this code:
IWebElement element = driver.FindElement(By.LinkText("Timesheets"));
element.Click();
Hope this will work for you!
Please let me know if you have any concerns related to this.

How to click on the first index in search drop down using selenium C#

IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://www.yahoo.com");
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
driver.Manage().Window.Maximize();
driver.FindElement(By.XPath("//*[#id='uh-search-box']")).SendKeys("selenium");
Thread.Sleep(5000);
IList<IWebElement> list = driver.FindElements(By.XPath("//*[starts-with(#id,'yui_3_18_0_6_1472')]"));
for (int i=0; i<list.Count; i++)
{
IWebElement element = list[1];
element.Click();
}
}
Need to click on the first index (first suggested result in search field)
the code above does not work please help
http://screencast.com/t/gbWTiXzuDmC
Try to use xPath like this //*[#role='option'] and get first item in WebElements list

How to scroll up using Protractor.NET?

My web page is little lengthy and the SAVE button is at the top right-hand corner. As I input the data through Protractor.NET, the webpage scrolls down which hides the SAVE button, thereby throwing a Element is not clickable at a point error. Now inorder to save the webpage, I need to scroll up and then find the SAVE button and click it.
I have an example in Protractor which uses window.scrollTo(0,0), but how do I implement the same in Protractor.NET
EDIT: Included code
public void Test()
{
var saveBtn = NgWebDriver
.FindElement(By.ClassName("btnSave"))
.FindElement(By.ClassName("Save"));
var btnSv = Scroller(saveBtn);
btnSv.Click();
}
public IWebElement Scroller(IWebElement element)
{
((IJavaScriptExecutor)NgWebDriver).ExecuteScript("arguments[0].scrollIntoView();", element);
return element;
}
So the exception occurs in Scroller method while casting the NgWebDriver to IJavaScriptExecutor type
How can I accomplish this?
Finally got the solution to scrolling up to the top in Protractor.NET
Had referred the link and was able to solve my problem.
The below code worked for me.
IWebDriver driver = MyWebDriver.WrappedDriver;
IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
jse.ExecuteScript("scroll(0, -250);");
Why do you want to make it complicated?
If you want to scroll to an element you can use this simple method:
public IWebElement ScrollToElement(IWebElement webElement)
{
((IJavaScriptExecutor)Driver).ExecuteScript("arguments[0].scrollIntoView();", webElement);
return webElement;
}
The below code helped me to achieve the same.
IJavaScriptExecutor jse = (IJavaScriptExecutor)NgDriver;
jse.ExecuteScript("arguments[0].scrollIntoView()", webElement);

How to scroll to element with Selenium WebDriver

How do I get Selenium WebDriver to scroll to a particular element to get it on the screen. I have tried a lot of different options but have had no luck.
Does this not work in the C# bindings?
I can make it jump to a particular location ex
((IJavaScriptExecutor)Driver).ExecuteScript("window.scrollTo(0, document.body.scrollHeight - 150)");
But I want to be able to send it to different elements without giving the exact location each time.
public IWebElement Example { get { return Driver.FindElement(By.Id("123456")); } }
Ex 1)
((IJavaScriptExecutor)Driver).ExecuteScript("arguments[0].scrollIntoView(true);", Example);
Ex 2)
((IJavaScriptExecutor)Driver).ExecuteScript("window.scrollBy(Example.Location.X", "Example.Location.Y - 100)");
When I watch it, it does not jump down the page to the element, and the exception matches the element being off screen.
I added an bool ex = Example.Exists(); after it and checked the results.
It does Exist (its true).
Its not Displayed (as its still offscreen as it has not moved to the element)
Its not Selected ??????
Someone is seeing success By.ClassName.
Does anyone know if there is a problem with doing this By.Id in the C# bindings?
Its little older question, but I believe that there is better solution than suggested above.
Here is original answer: https://stackoverflow.com/a/26461431/1221512
You should use Actions class to perform scrolling to element.
var element = driver.FindElement(By.id("element-id"));
Actions actions = new Actions(driver);
actions.MoveToElement(element);
actions.Perform();
This works for me in Chrome, IE8 & IE11:
public void ScrollTo(int xPosition = 0, int yPosition = 0)
{
var js = String.Format("window.scrollTo({0}, {1})", xPosition, yPosition);
JavaScriptExecutor.ExecuteScript(js);
}
public IWebElement ScrollToView(By selector)
{
var element = WebDriver.FindElement(selector);
ScrollToView(element);
return element;
}
public void ScrollToView(IWebElement element)
{
if (element.Location.Y > 200)
{
ScrollTo(0, element.Location.Y - 100); // Make sure element is in the view but below the top navigation pane
}
}
This works for me:
var elem = driver.FindElement(By.ClassName("something"));
driver.ExecuteScript("arguments[0].scrollIntoView(true);", elem);
This works for me in C# automation:
public Page scrollUp()
{
IWebElement s = driver.FindElement(By.Id("your_locator")); ;
IJavaScriptExecutor je = (IJavaScriptExecutor)driver;
je.ExecuteScript("arguments[0].scrollIntoView(false);", s);
return this;
}
I created a extension for IWebDriver:
public static IWebElement GetElementAndScrollTo(this IWebDriver driver, By by)
{
var js = (IJavaScriptExecutor)driver;
try
{
var element = driver.FindElement(by);
if (element.Location.Y > 200)
{
js.ExecuteScript($"window.scrollTo({0}, {element.Location.Y - 200 })");
}
return element;
}
catch (Exception ex)
{
return null;
}
}
For scroll down inside the page here I have small code and solution
My Scenario was until I scroll down the page. Accept and Don't accept button was not getting enabled. I was having 15 terms and conditions from which I needed to select 15th term and condition by inspecting webpage and taking the Id of last terms and condition paragraph.
driver.FindElement(By.Id("para15")).Click();
<div id="para15">One way Non-Disclosure Agreement</div>
I had somehow same problem. I was working on a web page and need to click on a button on a child window which by default, was located below screen.
This is the code I used and it worked.
Actually I just simulated a mouse drag and drop and moved the window 250 points upwards so that the button was in the screen.
Actions action = new Actions(driver);
action.DragAndDropToOffset(driver.FindElement(By.XPath("put an element path which **is in the screen now**, such as a label")), 0, -250);
action.Build().Perform();
If the reason we put time is for a long time to load the page, we put it. Just it.
ChromeOptions options = new ChromeOptions();
var driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://www.w3schools.com/");
Thread.Sleep(5000);
driver.ExecuteScript("scroll(0,400)");
HtmlDocument countriesDocument = new HtmlDocument();
countriesDocument.LoadHtml(driver.PageSource);
I am providing solution to scroll within a specific element, like a scrollable table.
// Driver is the Selenium IWebDriver
IJavaScriptExecutor exec = (IJavaScriptExecutor) Driver;
int horizontalScroll= direction == Direction.Right ? X : 0;
int verticalScroll = direction == Direction.Down ? Y : 0;
exec.ExecuteScript(
"arguments[0].scrollBy(arguments[1], arguments[2])"
, Self
, horizontalScroll
, verticalScroll);
Actions actions = new Actions(driver);
actions.SendKeys(Keys.PageDown).Build().Perform();
You do it through for, it works like clockwork, simple, but not always convenient
var js = (IJavaScriptExecutor)driver;
js.ExecuteScript("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center'})", PutYourElementIDHere);
var e = driver.FindElement(By.XPath("//*[text()='Timesheet']"));
// JavaScript Executor to scroll to element
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", e);

Categories

Resources