I am trying to access a radio button. From my research I found the element using the name, since the elements of a radio button have the same name. I want to select the second button, but I get an element not visible exception and the location property of the element returned is outside the window. However, I can see the element on the page.
The exception: An exception of type 'OpenQA.Selenium.ElementNotVisibleException' occurred in WebDriver.dll but was not handled in user code
ReadOnlyCollection<IWebElement> webElements = webElement.FindElements(By.Name("thename"));
webElements[1].SendKeys(Keys.Return);
I tried to move to location using the execute javascript as well as the Actions method. Neither worked.
I tried all possible keys to send to click for the Actions method:
Actions actions = new Actions(webDriver);
actions.MoveToElement(webElements[1]);
actions.SendKeys(Keys.Return);
actions.SendKeys(Keys.Enter);
actions.SendKeys(Keys.ArrowDown);
actions.Click();
actions.Perform();
For the javascript executor I tried two methods. To scroll into view, and to scroll by the amount that the Location property of webElement[1] told me it was off by.
IJavaScriptExecutor js = webDriver as IJavaScriptExecutor;
string title = (string)js.ExecuteScript("arguments[0].scrollIntoView(true);", webElements[1]);
string title1 = (string)js.ExecuteScript("javascript:window.scrollBy(557329,136);");
I still keep getting the location not visible exception.
A quick check you could do is open the developer tools in browser and in the console see if document.querySelector("[name='thename']") finds the element. If not, maybe that element is in an iframe or the name you are using may have a typo. You will also want to make sure that the radio button you are trying to access is below 'webElement' in the DOM tree. It's also possible that the page is still loading when you are checking for the element. To check for this put in a wait before trying to find the element so the page can load fully.
Related
The web page https://www.priceline.com/?tab=cars&vrid=7fb0c3635c8e8e7633afe152907a052e has an <input> element. When I click on it and start typing a <div> with a list of items below appears and I can choose from that list. But when I insert text into that <input> element i.e. the location field, where you see "CIty, Airport or Address" placeholder element on the webpage opened by Selenium, either by actually typing myself or via driver.FindElement(...).SendKeys(...), I see the text, but the list below is not showing.
I don't even know how to approach this. Do I need to configure the driver in a special way?
I assume there is some javascript that intercepts the typing and shows the list below. But, what can be the difference between typing in real life and through Selenium? What can I do?
Selenium Version
Selenium.WebDriver - 4.8.0
Selenium.WebDriver.ChromeDriver - 110.0.5481.7700
It is possible, the way you are doing it is not simulating typing into the input so the event listener that is listening for that simulation isn't being fired. Try this code below which simulates someone actually typing.
using OpenQA.Selenium.Interactions;
// Find the input element
IWebElement inputElement = driver.FindElement(By.Id("input-element-id"));
// Create an Actions object and send keys to the input element
Actions actions = new Actions(driver);
actions.MoveToElement(inputElement)
.Click()
.SendKeys("your text here")
.Perform();
The site is detecting Selenium use and blocking use of the site.
If you navigate to the site in a normal browser, typing in the search field works fine.
If you navigate to the site using Selenium and then use the site manually, the dropdown functionality still doesn't work. If you refresh the page, you get sent to a reCATPCHA page with the message, "Access to this page has been denied because we believe you are using automation tools to browse the website."
The desired element is a dynamic element, so to send some text e.g. Boston within City, Airport or Address field and click on the matching option you need to induce WebDriverWait for the ElementToBeClickable() and you can use either of the following Locator Strategies:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[#data-testid='startLocation-typeahead-input']"))).SendKeys("Boston");
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[#data-testid='typeahead-dropdown-card']//div[#role='option' and contains(., 'Boston')]"))).Click();
Browser Snapshot:
I'm trying to select page element with no unique id or class. Let it be radiobutton on this page. When I try to use name to find the radiobutton
var radioButton = driver.FindElements(By.Name("gender"))[1];
radioButton.Click();
Visual Studio returns me "An unhandled exception of type System.ArgumentOutOfRangeException' occurred in mscorlib.dll".
Timeout was added to make sure that page is fully loaded. But that was not the solution.
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
I tried to avoid XPath usage but that didn't work either and another exception occurred
var radioButton = driver.FindElement(By.XPath("/html/body/form/input[2]"));
radioButton.Click();
So I'm trying to figure out:
What is the proper way to select the radiobutton in this case?
How to get rid of OutOfRange exception?
Why XPath didn't work in this case?
Any help much appreciated.
As long as the page isn't full of radio buttons with the same values, you can use a CSS selector like input[value='male'].
The issue is caused by iframe. Add this code to be able to select elements inside one. If there's no name in corresponding iframe please see this discussion
driver.SwitchTo().Frame(FrameName);
I'm trying to run some automated tests in Sitecore 8.1 using Chrome and Selenium and c#. My code doesn't want to find any elements within the Sitecore pages, specifically the experience editor.
I am encountering the "unable to locate element" warning.
For eg: an item I want to .Click() is the toolbar ribbon button to expose the toolbar menu.
Here's the element:
<a data-sc-id="QuickRibbon" data-sc-click="trigger:button:toggleshow" data-sc-command="" data-sc-сontrolstaterequest="" data-sc-controlstateresult="" data-sc-postponedcall="" data-sc-ispressed="false" class="sc-quickbar-item sc-quickbar-button sc_QuickbarButton_53 data-sc-registered" title="Toggle the ribbon." data-sc-pagecodescriptfilename="" data-bind="ispressed: isPressed, visible: isVisible, click: click, command: command, enabled: isEnabled" data-sc-require="/-/speak/v1/ribbon/QuickbarButton.js" href="#" style="float:right"><img src="/sitecore/shell/client/Speak/Assets/img/Speak/Common/16x16/white/navigate_down.png" alt="Toggle the ribbon."></a>
Here's its XPath:
/html/body/div/div/div[1]/nav[1]/a[3]
I have extended the wait time to allow it to become visible as it can take a few seconds to load these pages. But this didn't work.
I have tried:
driver.FindElement(By.XPath("/html/body/div/div/div[1]/nav[1]/a[3]/img")).Click();
which gave me the "unable to locate" error
driver.findElement(By.className("class="sc-quickbar-item sc-quickbar-button sc_QuickbarButton_53 data-sc-registered"")).Click();
which gave me an error about unable to use compounded classnames.
I've tried a whole host of other options/combinations trying to pick up the alt text etc but I just can't get it to pick up the element.
Any ideas? Let me know if you need any more info.
Thanks
Change this line
driver.findElement(By.className("class="sc-quickbar-item sc-quickbar-button sc_QuickbarButton_53 data-sc-registered"")).Click();
to below line :-
driver.findElement(By.className("sc-quickbar-item sc-quickbar-button sc_QuickbarButton_53 data-sc-registered")).Click();
Edited 1..
If compound class does not work here you can perform action by using xpath as below :-
var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
var clickableElement = wait.Until(ExpectedConditions.ElementToBeClickable(By.xpath("//a[#data-sc-id='QuickRibbon']")));
clickableElement.Click();
Edited 2..
You need to switch frame before perform action if your element is present inside a frame as below :-
driver.SwitchTo().Frame("your frame name or id");
Hope it will work...:)
following suggestions from #Software_engineer I have managed to write this which works:
Thread.Sleep(6000);
driver.SwitchTo().Frame(driver.FindElement(By.Id("scWebEditRibbon")));
var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
var clickableElement = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[#data-sc- id='QuickRibbon']")));
clickableElement.Click(); //click to drop down the toolbar
driver.SwitchTo().DefaultContent();
I needed to switch to the iframe!
I am making use of the GeckoWebbrowser control in a C# Windows forms app environment.
I am wanting to call the click event on a button on the browser page, from a code event behind.
When I try an access a specific button I can find it by making use of the GetElementById command, however after assigning this information to the GeckoInputElement to call the click event, there is a null reference exception visible with this.
My code call to get the element looks like this:
GeckoInputElement betbt = new GeckoInputElement(wBrowser.Document.GetElementById("bet-bt").DomObject);
If I assign it like this I can access the element but still cannot click it with the GeckoElement object:
GeckoElement g1 = (GeckoElement)wBrowser.Document.GetElementById("bet-bt");
The HTML for the button looks as follows:
<button data-action="bet" id="bet-bt" class="action">Bet</button>
GeckoInputElement's are for Html Input elements.
Just use GeckoHtmlElement:
GeckoHtmlElement g1 = (GeckoHtmlElement)wBrowser.Document.GetElementById("bet-bt");
GeckoElement and GeckoHtmlElement exist to differentiate when the browser is displaying non html.
For Html documents always use GeckoHtmlElement.
I have a set of common methods which I run for each test. Like I need to add a few items in my cart and the whole test runs for each item. But for the first item it runs fine and when the process is repeated for the second item it fails while verifying a text and I get a StaleElementReferenceException.
How do I look up the item again or solve this ? Thanks.
Code where it fails:
public bool VerifyItemPresentInCart()
{
//Get the cartsize and verify if one item present
IWebElement cartSize = driver.FindElement(By.CssSelector("div[class='cart-size']>div"));
string actualMsg = cartSize.Text;
string expectedMsg = "1";
VerifyIfTextPresentMethod(expectedMsg,actualMsg);
return true;
}
Error at
IWebElement cartSize = driver.FindElement(By.CssSelector("div[class='cart-size']>div"));
Update: html code
<a class="header-button show-cart has-cart-items" data-view-name="cart-badge" data-view-cid="view5" data-model-cid="c6" data-tappable="true">
Cart
<div class="cart-size">
<div>3</div>
</div>
New code:
IWebElement cardDetails = driver.FindElement(By.CssSelector("div[class='form-field clear-fix']>label[for='cardNumber']>div"));
I would try joining your lines of code:
IWebElement cartSize = driver.FindElement(By.CssSelector("div[class='cart-size']>div"));
string actualMsg = cartSize.Text;
So that they are:
string actualMsg = driver.FindElement(By.CssSelector("div[class='cart-size']>div")).Text;
This means that the text will be retrieved on the element as it is selected. I am wondering if between getting a handle on the element via its parent and retrieving the text you are losing focus on that element. Alternatively, remove the >div from your css and see if it retrieves the text anyway.
This not working suggests the situation you are facing is the point under the title The Element is not Attached to the DOM. The fact that your target text is within only divs suggests that this area is being styled by javascript and thus may only be active at certain times. If this element is not active, but is accessible, you can still receive the StaleElementReferenceException, as indicated at that page. Your next step would be to see if you can click a parenting div to activate this target div prior to accessing its text (eg. make sure the element is attached to the DOM then call the code you have provided).