Selenium throws "OpenQA.Selenium.NoSuchElementException" in C# program - c#

I'm programming Selenium using C#, but I don't understand how to use it.
For example, I want to check the XPath that I want to click from HTML.
This is the HTML I want.
<span class="option_text">10</span>
Below is the original HTML
<div class="report_options _options">
<a href="#" class="option" data-reportpolicy="R1">
<i class="ico as_radio"></i>
<span class="option_text">1</span>
</a>
<a href="#" class="option" data-reportpolicy="R2">
<i class="ico as_radio"></i>
<span class="option_text">2</span>
</a>
.
.(Omitted)
.
<a href="#" class="option" data-reportpolicy="R11">
<i class="ico as_radio"></i>
<span class="option_text">10</span>
</a>
</div>
I used
WebDriver.FindElement(By.XPath("//span[contains(#class,'option_text') and contains(text(), '10')]")).Click();
but I could not. How can I fix this?
PS. Visual Studio error log:
OpenQA.Selenium.NoSuchElementException: 'no
such element: Unable to locate element:
{"method":"xpath","selector":"//*[#id='report-question-layer']/div[2]/div/div[3]/div/div[2]/div/a[10]"}
(Session info: chrome=60.0.3112.101) (Driver info:
chromedriver=2.31.488763
(092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT
10.0.14393 x86_64)'

Use below XPATH :-
//a[#class='option' and #data-reportpolicy='R11']//span[#class='option_text']
OR
//*[#id="report-question-layer"]/div[2]/div/div[3]/div/div[2]/div/a[10]/span
OR
//div[#id='report-question-layer']//div[#class='report_options _options']//a[#data-reportpolicy='R11']/span
OR
//a[#data-reportpolicy='R11']/span
Hope it will help you :)

Checking in the HTML, You can try to use this way:
First, try to switch to the frame:
WebDriver.SwitchTo().Frame(driver.FindElement(By.Name("fb_xdm_frame_https")));
Then try the following XPath to click on it:
WebDriver.FindElement(By.XPath("//span[contains(text(), 'その他の禁止されている行為')]")).Click();

Related

How to click on a dropdown option using C# selenium

I need to click on one of the dropdown options in order to expand page entries from 10 to 50. HTML looks like this:
<button id="page-size-dropdown-toggle" class="btn btn-white btn-sm dropdown-toggle ng-binding" data-toggle="dropdown" role="menuitem" aria-label="Change page size">
10
<i class="fa fa-chevron-down" role="presentation"></i>
<span class="sr-only ng-binding">Shown</span>
</button>
<ul id="page-size-dropdown-toggle-menu" class="dropdown-menu" role="menu" aria-labelledby="page-size-dropdown-toggle">
<!-- ngRepeat: size in pageSizeCtrl.getPageSizes() --><li ng-repeat="size in pageSizeCtrl.getPageSizes()" role="presentation" class="ng-scope">
<a href="" ng-click="pageSizeCtrl.changePageSize(size)" class="menuitem" role="menuitem" aria-label="Show 10">
<span aria-hidden="true" class="ng-binding">10</span>
</a>
</li><!-- end ngRepeat: size in pageSizeCtrl.getPageSizes() --><li ng-repeat="size in pageSizeCtrl.getPageSizes()" role="presentation" class="ng-scope">
<a href="" ng-click="pageSizeCtrl.changePageSize(size)" class="menuitem" role="menuitem" aria-label="Show 25">
<span aria-hidden="true" class="ng-binding">25</span>
</a>
</li><!-- end ngRepeat: size in pageSizeCtrl.getPageSizes() --><li ng-repeat="size in pageSizeCtrl.getPageSizes()" role="presentation" class="ng-scope">
<a href="" ng-click="pageSizeCtrl.changePageSize(size)" class="menuitem" role="menuitem" aria-label="Show 50">
<span aria-hidden="true" class="ng-binding">50</span>
</a>
</li><!-- end ngRepeat: size in pageSizeCtrl.getPageSizes() -->
</ul>
</div>
As you can see there are 3 options 10, 25 and 50. Here what I have tried so far:
clicking of the drop down and using LinText - Cant find this element
var dropDown = driverIpass.FindElement(By.Id("page-size-dropdown-toggle"));
dropDown.Click();
driverIpass.FindElement(By.LinkText("Show 50")).Click();
using XPath (unclickable element) tried different paths as well, no avail
IWebElement element = driverIpass.FindElement(By.XPath("//*[#class='dropdown-menu']/li[3]/a"));
element.Click();
As well as XPath using #id
Also tried digging around within classes hoping to find the correct link - nothing. There is something I don't see/realize.
It's my second day using C# and selenium so if anyone could put me on the right path I would really appreciate it.
I cant provide the actual web page as this is internal (company)
Try with WebDriverWait() and following css selector.
WebDriverWait wait = new WebDriverWait(driverIpass, TimeSpan.FromSeconds(10));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.CssSelector("#page-size-dropdown-toggle"))).Click();
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.CssSelector("#page-size-dropdown-toggle-menu li a[aria-label='Show 50']"))).Click();
Found an answer after posting my question. In case someone else might have a similar problem:
//click to open drop down menu
var dropDown = driverIpass.FindElement(By.Id("page-size-dropdown-toggle"));
dropDown.Click();
//select the 3rd option within menu
IWebElement element = driverIpass.FindElement(By.XPath("//*[#id='page-size-dropdown-toggle-menu']/li[3]/a"));
element.Click();

C# Selenium webdriver button click

I am trying to click this button through Selenium web driver but it whatever I try, it says cannot find element.
<a class="button" href="#" id="enrollForm">ENROLL NOW</a>
I tried
driver.FindElement(By.XPath("//*[#id='enrollForm']")).Click();
Update:
This is the whole snippet. I want to click on the button "Enroll Now":
<div class="buttonContainerLanding">
<div class="buttonDiv">
<a class="button" href="#" id="enrollForm">ENROLL NOW</a>
</div>
<div class="buttonDiv">
<!-- <b class="buttonTitle">Need to Activate Your Card?</b> -->
<a class="button" href="#" id="activate">ACTIVATE CARD</a>
</div>
<div class="buttonDiv">
<!-- <b class="buttonTitle">Need to Activate Your Card?</b> -->
<a class="button" href="#" id="replace">REPLACE CARD</a>
</div>
</div>
To click on the element with text as ENROLL NOW you can use either of the following solutions:
Using LinkText:
driver.FindElement(By.LinkText("ENROLL NOW"));
Using CssSelector:
driver.FindElement(By.CssSelector("div.buttonContainerLanding div.buttonDiv>a.button#enrollForm"));
Using XPath:
driver.FindElement(By.XPath("//div[#class='buttonContainerLanding']//div[#class='buttonDiv']/a[#id='enrollForm' and text()='ENROLL NOW']"));

How to click on an icon through xpath consistently using Selenium

Please see below the xpath expression copied from the developer tools.
//*[#id="parentDiv"]/table/tbody/tr[1]/td[8]/div/profile-link-column/a/i
I want to click on the 8th table data in first row(actually its a small icon), so this works fine 3 out of 10 times.
Can somebody suggest a more reliable approach?
This is my HTML:
<td>
<!--anchor-->
<div class="animated-slide-in au-enter-active">
<profile-link-column device-id="${value.value}" class="au-target" au-target-id="395">
<a click.trigger="viewProfile()" class="au-target" au-target-id="55">
<i class="fa fa-newspaper-o"></i>
</a>
</profile-link-column>
</div>
<!--anchor-->
<!--anchor-->
<!--anchor-->
<!--anchor-->
<!--anchor-->
</td>
Use css here
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.cssSelector("i.fa.fa-newspaper-o")).click
check whether it works fine, if it doesn't work, then please paste the error, If it's related to visibility error, then I think the problem is, <profile-link-column device-id="${value.value}" class="au-target" au-target-id="395"> <a click.trigger="viewProfile()" class="au-target" au-target-id="55"> <i class="fa fa-newspaper-o"></i> </a> </profile-link-column> is hidden in your html.
To click on the desired element you have to induce WebDriverWait for the element to be clickable as follows :
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//td/div[#class='animated-slide-in au-enter-active']/profile-link-column[#class='au-target']/a[#class='au-target']/i[#class='fa fa-newspaper-o']"))).Click();

Unable to click button in popup using webdriver

I am trying to click a button using selenium webdriver. Working fine with the following XPath
driver.FindElement(By.XPath("html/body/div[36]/div[3]/div/button[1]")).click();
it clicks the button fine but if I try to find it using class then it wont click it
driver.FindElement(By.XPath("//div[#class='ui-dialog-buttonset']/button[1]")).click();
Any Idea what I am doing wrong. Actual source code is as follows:-
<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
::before
<div class="ui-dialog-buttonset">
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" role="button" aria-disabled="false">
<span class="ui-button-text"></span>
</button>
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" role="button" aria-disabled="false">
<span class="ui-button-text"></span>
</button>
</div>
::after
</div>
</div>
I see two buttons with the same class name. You could try this:
List<WebElement> list = driver.findElements(By.cssSelector("button[class=\"ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only\"]"));
//click on the first button
list.get(0).click();
you can try the contains operator "*" of css selector.
List<WebElement> mylist = driver.findElements(By.cssSelector("button[class*='ui-corner-all']"));
mylist[0].click();
you can visit this link for CSS Selector tutorial in Selenium with c#
http://binaryclips.com/2015/02/16/css-selectors-for-selenium-webdriver/

Why is the Razor parser complaining about a tag being left open?

It is saying I need to properly close my div tags. But they are opened and closed properly unless I am just completely missing something here.
Anyone familiar with why I might be getting this error?
<div id="attachments" class="popoversample">
<h3>
Attachments</h3>
<div class="peoplelist">
#foreach (var item in Model.documents)
{
<div class="peoplewrapper">
<div class="thumb">
<img src="../../Images/thumbs/doc.png" alt="" /></div>
<div class="peopleinfo">
<h4>
<a href="~/Documents/#Html.DisplayFor(modelItem => item.path)" target="_blank">
</h4>
<img src="../../Images/thumbs/doc.png" alt="" /></a> <span class="filename">#item.filename</span>
<ul>
<li><span>Followers:</span> 10 / <span>Following:</span> 21</li>
<li><span>Member:</span> April 2011</li>
<li><span>Skype:</span> johndoe</li>
<li><span>Phone:</span> +44033 0400 332</li>
<li><span>Address:</span> Something St., Some City, Place 4023</li>
</ul>
</div>
</div>
<!--peoplewrapper-->
}
</div>
</div>
Error: Parser Error Message: Encountered end tag "div" with no matching start tag. Are your start/end tags properly balanced?
Fix your HTML, there's an error here; the h4 should not end in the middle of an anchor.
<a href="~/Documents/#Html.DisplayFor(modelItem => item.path)" target="_blank">
</h4>
<img src="../../Images/thumbs/doc.png" alt="" /></a>
Looks like this <a> tag might be causing the problem.
<a href="~/Documents/#Html.DisplayFor(modelItem => item.path)" target="_blank">
I presume the closing </h4> should be moved down a line below the image/span and closing </a> tag
In ASP.NET you can open a tag inside the block and must close it too before opening a new one.
Your code seems pretty bug free! But you need to remember that you need to close the block of div or the server side language before you start something new.
Else this error might pop up. Now the issue there might be in the peopleWrapper.
One more bug that is present is. You can see this:
<h4>
<a href="~/Documents/#Html.DisplayFor(modelItem => item.path)" target="_blank">
</h4>
<img src="../../Images/thumbs/doc.png" alt="" /></a>
You are opening an <h4> block but closing it before closing the <a>. You should check all the links or codes present there. The issue can be closing opening of any tag.
The real issue is here (simplified version to highlight the structure):
<h4>
<a>
</h4>
<img /></a>
The <a> tag is not properly closed, because of where the h4 closes. Maybe you meant to have:
<h4>
<a href="~/Documents/#Html.DisplayFor(modelItem => item.path)" target="_blank">
<img src="../../Images/thumbs/doc.png" alt="" />
<span class="filename">#item.filename</span>
</a>
</h4>

Categories

Resources