When I inspected for a scroll bar it pointed the element .x-table-container, tried to scroll with the following code, but it's not working, is there any other solution for this?
protected void DragAndDropToVertical(IWebElement webElement, int dragValue)
{
new Actions(Driver).DragAndDropToOffset(webElement, 0, dragValue).Build().Perform();
PauseExecution(200);
}
The problem with Action chain it is usually not working when there is an iframe html in main body or element is not in view. So i think you should use Javascript Execute instead, this usually work best for me. Try it your self:
WebElement element = driver.findElement(By.id("find element with id or using what ever you want"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(500);
This code will scroll until it find your element.
Reference
Scroll Element into View with Selenium
Related
I am getting the following error while trying to click on a button which is present at the bottom of the page. Looks like this error is coming because that button is not visible at the first point, if we scroll the page down then only selenium is able to identify that button.
OpenQA.Selenium.ElementClickInterceptedException : element click intercepted: Element <span>...</span> is not clickable at point (1113, 659)..
I have tried with following code to scroll down the web page but it does not help me.
Actions actions = new Actions(driver);
actions.SendKeys(Keys.ArrowDown);
If I try with element.Sendkeys(Keys.ArrowDown) then also its not helping.
what would be the correct approach here?
You should use Actions class to perform scrolling to the element in this way
Actions actions = new Actions(driver);
var element = driver.FindElement(...);
actions.MoveToElement(element);
actions.Perform();
substitute ... with the selector for the element.
There is also another method relying on javascript
IJavaScriptExecutor js = (IJavaScriptExecutor) driver;
js.ExecuteScript("arguments[0].scrollIntoView();", element);
I am having a really hard time with the following issue. I am trying to navigate through some web pages that have various inputs (text boxes/dropdowns/buttons) followed by a continue button at the bottom to move onto the next screen. My tests frequently fall over because they can't always locate the first element in order to interact with it.
As far as I'm aware the page doesn't do any fancy AJAX post loading or anything, so once the page has loaded then Webdriver should be able to locate every element.
My code:
FRAMEWORK
public static void WaitOnPageForXPathClickable(string selector)
{
new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable((By.XPath(selector))));
}
TEST CASE
Utilities.WaitOnPageForXPathClickable("the xpath of the continue button and checks that it is clickable");
Driver.Instance.FindElement(By.XPath("the xpath of the first button that I want to click")).Click();
Do I need to try and include a function to ensure that the page is fully loaded before test execution? I am confused about this because I have read that Selenium already waits for the page to load by default. I would have thought that waiting for one element (the continue button) to be clickable should mean that all other elements are ready by then too? I really want to avoid having to wait for every single element before clicking it.
Can you please share html, have you tried with Visible instead of Clickable
public static void WaitOnPageForXPathClickable(string selector)
{
new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible((By.XPath(selector))));
}
You can wait for the page to be fully loaded with something like:
public void WaitForPageToBeFullyLoaded(int timeSpan)
{
new WebDriverWait(Driver, TimeSpan.FromSeconds(timeSpan)).Until(
d => ((IJavaScriptExecutor) d).ExecuteScript("return document.readyState").Equals("complete"));
}
You can check for displayed or Enabled element too.
You have made things much more complicated by waiting for the Continue Button Utilities.WaitOnPageForXPathClickable("the xpath of the continue button and checks that it is clickable"); where as in the next step trying to click() on the First Button Driver.Instance.FindElement(By.XPath("the xpath of the first button that I want to click")).Click();
Test Scenario:
Ideally your Test Scenario should have been :
TEST CASE:
Utilities.WaitOnPageForXPathClickable("the xpath of the first button that I want to click");
Driver.Instance.FindElement(By.XPath("the xpath of the first button that I want to click")).Click();
FRAMEWORK:
public static void WaitOnPageForXPathClickable(string selector)
{
IWebElement element = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable((By.XPath(//label[#class='replaced-input-label replaced-input-label--radio']))));
elementClick();
}
I was not able to get satisfactory answer after searching on google.
So could you please guide me on this?
I have a div containing li,a,labels below it.
I am able to find the div using CssSelector by its class name.
Now inside this div I want to get a label with its text and then click on it.
The label is as belo:
<label>Sign Out</label>
How to do that ?
I have a working solution using XPath and iterating over all labels inside div, but I am unable to get it using CssSelector.
My Solution:
IWebElement menu = CurrentDriver.FindElement(By.CssSelector("div[class='menu-panel right']"));
IWebElement logoutLabel = menu.FindElement(By.XPath("//label[text()='Sign Out']"));
or
by using foreach:
var coll = menu.FindElements(By.TagName("label"));
foreach (var label in coll)
{
if(label.Text.Trim() =="Sign Out")
{
Log("Sign out was found.");
label.Click();
break;
}
}
I tried with CssSelector:
IWebElement logoutLabel = menu.FindElement(By.CssSelector(":contains('Sign Out')"));
IWebElement logoutLabel = menu.FindElement(By.CssSelector("label:contains('Sign Out')"));
IWebElement logoutLabel = menu.FindElement(By.CssSelector("label['Sign Out']"));
But these are not working.
There is a very good reason why your CSS selector wouldn't work, specifically the contains bit is where it falls over. Why?
It isn't part of the CSS selector specification, and therefore would never work.
The contains that we all know and love is actually coming from Sizzle, the CSS selector engine behind jQuery.
If you want text-based searching, you will either have to use XPath, or get a collection of those elements (using any locator you see fit) and then filter them down in code (like you have done in your foreach loop). There isn't a native CSS-style way to do "text based searching".
In terms of your current code, you will probably also fall over because XPath requires a little "poking" to tell it to search only the child elements of your current "element".
IWebElement menu = CurrentDriver.FindElement(By.CssSelector("div[class='menu-panel right']"));
IWebElement logoutLabel = menu.FindElement(By.XPath("//label[text()='Sign Out']"));
Should be:
IWebElement menu = CurrentDriver.FindElement(By.CssSelector("div[class='menu-panel right']"));
IWebElement logoutLabel = menu.FindElement(By.XPath(".//label[text()='Sign Out']"));
(Note the "." in the XPath)
Also, your foreach loop should have worked, therefore you will have to put a breakpoint in there and check exactly what is being returned by menu.FindElements(By.TagName("label"));.
You are using :contains(), which is a jQuery selector, not a CSS selector.
Apparently there is no way to achieve such thing using only CSS.
More info CSS selector based on element text?
I am trying to get the Y scroll index for a web page in the WebBrowser control but I can't access the values for the built in Scroll bar.
Any Ideas?
For IE in standards mode (with a doctype, as you say) scrollTop is a property of the <html> element, not the <body>:
HtmlDocument htmlDoc = this.webBrowser1.Document;
int scrollTop = htmlDoc.GetElementsByTagName("HTML")[0].ScrollTop;
(A nicer way to get to the <html> element would be good, if anyone knows of one.)
are you trying to target an HTML element to bring it into view? If that is what you are after you can do this...
htmlDoc.GetElementById("tag_id_string_goes_here").ScrollIntoView(true);
true aligns it with the top and false with the bottom of the element. You can also use ScrollRectangle to get the dimensions of the scrollable region.
WebBrowser1.Document.Body.ScrollTop;
WebBrowser1.Document.Body.ScrollRectangle.Height;
I know when i want to scroll down to specific element in appium use the following
driver.ScrollTo(value);
but this value is changed every time and can't detect it i can not use this value to scroll until find the element, but this element is the last element in my page and number of element in the page is changed between user and another.
So, there is any other way to scroll down till the end of the page ?
Use xpath to find the element without using value(as it is dynamic)
then use that element to scroll,
WebElement element = driver.findElementByXpath("xpath_of_element");
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Just post the DOM of that page..i'll give you efficient xpath so even if your dom is dynamic that locator will work...
scrollToExact and scrollTo method is depricated since JAVA client version 4.0.0 (https://github.com/appium/java-client/blob/master/README.md).
You can use UiScrollable and UiSelector and their methods to scroll any page (https://developer.android.com/reference/android/support/test/uiautomator/UiScrollable.html).
Example of JAVA code (please edit it with C# syntax):
#AndroidFindBy (uiAutomator = "new UiScrollable(new
UiSelector()).scrollIntoView(new UiSelector().textContains(\"Question
\"))")
If you want scroll to the bottom of page, please create method that will do several steps to find last element of page, otherwise you will get an error (Element not found)
Use touch action instead
TouchAction t = new TouchAction(driver);
t.longPress(element(source)).moveTo(element(destination)).release().perform();