Unable to select checkbox using Selenium webdrive (c#) - c#

I've been puzzling at this for a few hours now. Checked through similar issues on Stackoverflow but been unable to find a solution.
I have a checkbox I'm trying to use Selenium webdriver to check, but when I run the script, I don't get any error messages but the checkbox remains unchecked.
I'm using the line below to select and check the box
IWebElement checkBox = m_driver.FindElement(By.XPath("//div[2]/label/span"));
checkBox.Click();
I've copied the HTML from it below.
<div class="input-group single-option label-empty" >
<label class="" >
<input type="checkbox" name="privacy" value="true" required />
<span>I have read and understood the <a data-toggle="#privacy-terms"
data-group="privacy-terms">Privacy Policy</a> and <a data-toggle=
"#terms" data-group="privacy-terms">Terms and Conditions</a>. </span>
</label>
I'd really be grateful for some help. I'm pretty new to automation...and C#
Unfortunately, I am unable to post the url as it's a passworded client site. If I can post the HTML, that may help...
I've posted the html from the page in question but removed the client name :)
Link to HTML
If I select the checkbox through Chrome developer tools and copy the Xpath, I get this.
IWebElement checkBox = m_driver.FindElement(By.XPath("/html/body/div[1]/main/section/form/div[2]/label/input"));
I re-ran it and Selenium generated an error.
Message: OpenQA.Selenium.WebDriverException : unknown error: Element
is not
clickable at point (491, 593). Other element would receive the click:
... (Session info: chrome=71.0.3578.98) (Driver info:
chromedriver=2.45.615291
(ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT
10.0.17134 x86_64)
developer tools
Here is a screenshot of developer tools with the checkbox ticked. Could it be something to do with the 'span'?

You are clicking the SPAN not the INPUT that is the checkbox. This should work.
m_driver.FindElement(By.CssSelector("input[name='privacy']")).Click();
You may need a wait, depending on what is going on before the click.
Your error about element is not clickable could be any number of things. It could be popup blocking it, a floating DIV panel, a loading spinner, ... etc. You will need to deal with the popup (by closing it, etc.), floating DIV may require the page to be scrolled, or for the spinner a wait for the spinner to become invisible. It's hard to say without more information.

I figured it out. Instead of using "checkBox.Click();", I used "checkBox.SendKeys(Keys.Space);
and it works. Now I'm trying to do the same with the recaptcha which by definition should be difficult.

Related

Anglesharp HTML parser doesn't seem to be parsing document deep enough to access desired element

So I'm trying to scrape a website using AngleSharp and want to access a particular button that is nested deep in the site. I have logged out the parsed document html with document.DocumentElement.OuterHtml
but can only see so far into the document:
<div class="l-propertySearch-paginationAndSearchFooter" data-test="pagination">
<div data-bind="component: 'pagination'"></div>
</div>
</div>
However, when I inspect the page in the web browser, I can see the additional layers necessary to access the button:
As you can see, the div with the data-bind attribute title "component: 'pagination'" open up further but doesn't display this in the log - this is why, I suspect, I can't retrieve the element.
I've experimented with document.QuerySelectorAll("button" and get back a list of buttons but not the one I'm after - it's like the particular block I want doesn't exist. Any ideas what I'm doing wrong?
As far as I understand that button you are looking for is created with javascript and does not exist in original source code. That is the reason you can't access that button with anglesharp. Right click on website and click View page source (Ctrl + U on chrome) and look for your button there. That is what anglesharp sees not html inside inspect element.

Programming with Selenium to automate an ARIA:Listbox with c# and Visual Studio 2019

I am trying to automate a download function from a broker. I have been able to logon with id and pw, navigate to specific web pages, and select and collect items from the site. I am stumped, however, on how to select an ARIA:Listbox item in an embedded form. All the examples I have seen show windows forms listbox or HTML select lists which is not the way this www site is built.
I have been successful with the c# Selenium findelement By.ID for the listbox required, but the last error said it must be scrolled into view. I'm guessing that the item is not in view because I haven't got the ARIA Listbox click to work to display the list. I've tried clicking on the dropdown arrow, and on the cell itself but the list doesn't show up.
I need some pointers on what to try next.
Thanks
OK - here's some additional info:
I apologize if this doesn't present right - I'm just learning to use the site...
When I use the program code
browser_driver.FindElement(By.Name("OfxDownloadForm:downloadOption")).Click();
I get the message
OpenQA.Selenium.ElementNotInteractableException: 'Element could not be scrolled into view'
The relevant html follows:
<span id="OfxDownloadForm:downloadOption" role="presentation" onfocusjs="removingDuplicateContentFromMenu();;vg.validation.focus(this)" onchangejs="setWarnings('downloadOption');" onblurjs="vg.validation.blur(this)" class="vg-SelOneMenu" compname="selectOneMenu">
<div class="vg-SelOneMenuCont vg-SelOneMenuNoWrap vg-SelOneMenuFocusText vg-SelOneMenuHover" id="OfxDownloadForm:downloadOption_cont" aria-owns="menu-OfxDownloadForm:downloadOption">
<a href="javascript:void(0);" onclick="return false;" class="vg-SelOneMenuTrigger" id="OfxDownloadForm:downloadOption_aTag" role="button" aria-xpanded="false" aria-haspopup="true" aria-controls="menu-OfxDownloadForm:downloadOption" aria-labelledby="downLoadOptionText OfxDownloadForm:downloadOption_text" aria-disabled="false">
If I go after "OfxDownloadForm:downloadOption_aTag" with:
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.PresenceOfAllElementsLocatedBy(By.ID("OfxDownloadForm:downloadOption_aTag")));
browser_driver.FindElement(By.ID("OfxDownloadForm:downloadOption_aTag")).Click();
Nothing happens - the popup doesn't occur.
Hope this comes out ok!
After much research and reading, I figured out that I had to do a two step action in the program:
input_element = browser_driver.FindElement(By.Id("OfxDownloadForm:downloadOption_aTag"));
Actions actions = new Actions(browser_driver);
actions.MoveToElement(input_element);
actions.Perform();
input_element.Click();
I believe the FindElement method just located the element, but didn't reposition the mouse into the document on that element. MoveToElement method scrolled down the document and brought the element into the view, and then a click on the same element allowed the drop-down listbox to do its processing. Once I did the find and the move, I was able to select the appropriate dropdown item.

WebDriver - element is not clickable Chrome

I have following problem. I run test on Firefox and Chrome. On Firefox test run correctly but on Chrome SauceLabs give a message:
unknown error: Element is not clickable at point (717, 657). Other
element would receive the click: <div class="col-md-9 col-sm-12"
style="margin-top:8px;">...</div> (Session info: chrome=36.0.1985.125)
(Driver info: chromedriver=2.10.267521,platform=Windows NT 6.3 x86_64)
I choose element by unique css selector in both test in the same way:
driver.FindElement(By.CssSelector("button.btn-xs:nth-child(1)")).Click();
Any ideas what is wrong here?
I am assuming that you have the correct element you need, ie the XPath is correct.
Here are few ways out:
Try to Click on the parent element instead.
Try .Submit() instead of .Click()
Try to execute the JavaScript that will be executed on the OnClick event of the element you are trying to click.
I have used the 3rd way with success all the time.
Another one
Do a .SendKeys(Keys.Enter) on that element (or a Space key)
Since you've tagged the question as Google-Chrome too - I suppose that this is happening mostly with ChromeDriver. I had the same issues with one of my previous projects (Asp .Net MVC). I found that when some elements are not visible for this Driver if they are not in the screen_visible_area. Please note that they are loaded (HTML, CSS3, JS etc.) properly.
So after a lot of reading and testing, I found that my workaround is simply scroll to the WebElement - so it is in the visible part of the screen. Actually this issue was not for all elements and I didn't find better solution for it.
unknown error: Element is not clickable at point (..., ...)
Is not descriptive error for this case, because like you I also thought that is Selector-related.
Just to be full answer - I had the same problems with IEDriver too. My implementation was to use the Browser scroll down/up options and just "send the screen" where the problematic element is.
Simple JSExecutor code that you can use:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(110,350)", "");
or
jse.executeScript("scroll(0, 250);");
or
driver.executeScript("window.scrollBy(110,350)", "");
Other topic-related useful resources are here.
Update
When it comes to the .sendKeys() I also used the browser accessibility features. All you need to do is just count how many TAB clicks your test need in order to get to the targeted web_element. Then just call .click().
Try this simple code:
element.sendKeys(Keys.TAB);
or
element.sendKeys("\t")
or
Actions builder = new Actions(driver);
builder.keyDown(Keys.TAB).perform()
I realize this is a super old question, but it came up while searching a nearly identical problem in the present day. After attempting many of the fixes described here and getting new exceptions for my trouble (mostly stale element and http request timeouts) I stumbled across this issue on Selenium's GitHub.
As described in the post, Chrome had advanced beyond the abilities of my version of chromedriver.exe--my v2.30 driver had known issues with clicking elements due to changes in Chrome v61 scrolling mechanics. Updating to the latest chromedriver.exe solved all my problems.
tl/dr: ensure your version of chromedriver is compatible with the version of Chrome being tested.
I was getting issue that login button is not clickable in chrome even xpath was correct. after browsing many sites, i came to the solution - use .submit() instead of .click() and it worked perfectly.
driver.findElement(By.xpath("//button[#id='loginBtn']")).click();
driver.findElement(By.xpath("//button[#id='loginBtn']")).submit();
If you're doing anything complicated in your CSS, Chrome can get confused (this has happened to me) and think that the element you're trying to click is covered by another element even though this is not the case.
One way to resolve this is to help Chrome understand the situation correctly by adding z-indexes (you'll need to add relative or absolute positioning also) to unambiguously place the correct element on top of the other.
For me, it was creating this command instead.
driver.FindElementByXPath("id('gender1')").SendKeys(Keys.Space);
For my case, I was interacting with radio control.
I have had this problem on FF. This issue happens when your field is not in the view area. The slick way to resolve this issue is to zoom out your browser:
TheNotClickableField.SendKeys(Keys.Control + "-" + "-");
you might want to zoom out more or less according to your page size.
I.
If the button is on the bottom of the page, the following code could be used to get you to the bottom via JavaScript from where the click can be executed:
(driver as IJavaScriptExecutor).ExecuteJavaScript("window.scrollTo(0,document.body.scrollHeight - 150)");
The same action could be done via C# with the Actions class provided by Selenium.
This will get you to the bottom of the page----->
new Actions(Driver).SendKeys(Keys.End).Perform();
Keys.End could be switched with Keys.PageDown // Keys.Space
II.
If you want to get to the exact position of the element you can:
1.Get the element's Y location---> var elementToClick = driver.findElement(By.{Anything}(""));
2.Execute the following JS---> (driver as IJavaScriptExecutor).ExecuteScript(string.Format("window.scrollTo(0,{0})", elementToClickYLocation.Location.Y));
3.Click the element---> elementToClick.click();

selenium with ng-hide wont let me click and gives visibility error

So I have 2 buttons, one appears on pc browser and one on a mobile browser, and I am trying to click one.
<button class="A otherclasses" ng-hide="isMobile">Button A</button>
<button class="B otherclasses ng-hide" ng-hide="!isMobile">Button B</button>
When I try to grab A the one that is showing on pc browser I get this error:
OpenQA.Selenium.ElementNotVisibleException : element not visible
I grab A using this code:
IWebElement target =
_driver.FindElement(By.CssSelector("button[ng-hide=\"isMobile\"]"));
Thread.Sleep(500);
target.Click();
I also tried grabbing it by By.ClassName() and still no luck
The test is in Visual Studio C#.
The driver is chrome.
I am trying to avoid to reference the text 'Button A' since it changes based on language.
I've been googling and fumbling around and I cannot grab A even though I can see it visually.
Have you tried
_driver.FindElement(By.CssSelector("button:not(.ng-hide)")).Click();

site scraping: drop down controls and search button click

Imagine a web site, where there are 2 drop down controls like min age and max age. After I choose min age and max age, I would like to hit the search button. How do I get the search results into an output stream and then I could navigate the DOM HTML. I already know how to naviate through dom html via htmlagilitypack. I just do not know how to invoke the drop down make selections and invoke search button. I want to do all this from console application. Is it even possible? I am using C# visual studio 2010.
Thanks..
If the html looks like this:
<form action="someurl">
<select name="SomeOption">
<option value="val1">val1</option>
<option value="val2">val2</option>
</select>
</form>
And you want to submit the form with "val1", then you need to execute an HTTP POST operation to "someurl" including the argument SomeOption=val1. That is essentially what the browser is doing. Of course the form you are trying to simulate will be a bit more complicated.
Is that what you were asking?
BTW, if you want to see exactly what is going on when the browser submits the form, check out Fiddler, an excellent tool made by a colleague at Microsoft.

Categories

Resources