How to select the drop down using Selenium/NUnit C# - selenium

Here is my control:
<input name="ctl00$ContentMain$dockAlert$C$ucAlerts$ddlViewBy" type="text" class="rcbInput radPreventDecorate" id="ctl00_ContentMain_dockAlert_C_ucAlerts_ddlViewBy_Input" value="Active" readonly="readonly" autocomplete="off">
Here is how I am TRYING to access it:
(tried all 3)
SelectElement ddlViewBy = new SelectElement(driver.FindElement(By.Name("ddlViewBy")));
SelectElement ddlViewBy = new SelectElement(driver.FindElement(By.Id("ddlViewBy")));
SelectElement ddlViewBy = new SelectElement(driver.FindElement(By.Id("ctl00_ContentMain_dockAlert_C_ucAlerts_ddlViewBy_Input")));
I must be missing something simple.

The desired WebElement is a dynamic element, so to identify the element you have to induce WebDriverWait for the ElementIsVisible() and you can use either of the following Locator Strategies:
CssSelector:
SelectElement ddlViewBy = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("input.rcbInput.radPreventDecorate#ctl00_ContentMain_dockAlert_C_ucAlerts_ddlViewBy_Input")));
XPath:
SelectElement ddlViewBy = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//input[#id='ctl00_ContentMain_dockAlert_C_ucAlerts_ddlViewBy_Input' and #class='rcbInput radPreventDecorate']")));

Related

How to locate the element as per the given HTML

<input value="Order (ESHOP)" class="btn" name="order_eshop" title="Order (ESHOP)" type="button" onclick="Sfdc.logServer('CUSTOM_URL_BUTTON', {id: '00b41000002iuj6', name: 'Order_ESHOP'}, Sfdc.Logging.LogLevel.INFO);
openIntegration('/servlet/servlet.Integration?scontrolCaching=1&lid=00b41000002iuj6&eid=a0863000007SV1q&ic=1&isdtp=vw&linkToken=VmpFPSxNakF4T1MweE1TMHhNRlF5TVRvd09EbzBNaTQyT1RWYSx0eno3X1lSS1lOY1hORmtKb2ZvM3BxLFlXWmtNR0po', 'height=600,location=no,resizable=yes,toolbar=no,status=no,menubar=no,scrollbars=1', 1)">
I' am not able to find the xpath for this. I have same element on a single path Order (ESHOP). When I'am trying to use indexing it is not finding the correct xpath.
The desired element is a JavaScript enabled element so to locate/interact with the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.btn[name='order_eshop'][value='Order (ESHOP)'][title='Order (ESHOP)']")));
Using XPATH:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='btn' and #name='order_eshop'][#value='Order (ESHOP)' and #title='Order (ESHOP)']")));

The radio button is not getting selected with this HTML code for that. How to do that?

<input id="radio2" name="radioGroup" type="radio">
<label class="flRight" for="radio2">
::before
"Senior Citizen"
::after
</label>
WebElement senior = driver.findElement(By.id("radio2"));
senior.click();
Now the problem is the code is not able to click the desired element.
You need to induce WebdriverWait And to click on the radio button use JavaScript Executor since neither webdriver click nor action class is working
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement item=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#class='flRight' and #for='radio2']")));
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", item);
Try with this:
WebElement senior = driver.findElement(By.xpath(".//div[#class='radioBtn']/label[2]"));
WebElement close = driver.findElement(By.xpath(".//div[#id='nvpush_cross']"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOf(close));
close.click();
senior.click();
Code didnt worked before because there is popup you need to close it.
Use WebDriverWait:
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement senior = wait.until(ExpectedConditions.elementToBeClickable(By.id("radio2")));
senior.click();
After seeing the WebSite it looks like the input is not the element you want to click rather the label...
So just change the senior var to:
WebElement senior = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='RadioButton']//label[#for='radio2']")));
The best way to automate human actions in the case where the element is not clickable dew to other elements covering them is to use Actions:
WebElement senior = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='RadioButton']//label[#for='radio2']")));
Actions actions = new Actions(driver);
actions.moveToElement(senior).click().build().perform();
(using JavascriptExecutor and executeScript doesn't realy click... it just invokes the method that can be good but not for testing...)
As a last resort use JavascriptExecutor:
JavascriptExecutor jse= (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();", senior);
Please try below solution :
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#class='flRight' and #for='radio2']")));
Actions action=new Actions(driver);
action.moveToElement(element).click().perform();

How to store the value from the list to a variable in selenium webdriver

I am new to selenium webdriver. I want to extract the value from the Ul class and store it in a variable but i am not able to do so
this is what i tried
WebElement testuser = driver.findElement(By.cssSelector(".box ul.form li:nth-child(4)"));
div class="box"
ul class="form"
User Type
School Administrator
<li><h4>First Name</h4></li>
<li>Faleata</li>
Its saying unable to locate the elements
To handle dynamic element use WebDriverWait and try the below xpath.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='box']/ul[#class='form']//li[./h4[text()='First Name']]/following::li[1]")));
System.out.println(element.getText());
You can try this bellow code
WebDriverWait waitElmt = new WebDriverWait(driver, 60);
WebElement element = waitElmt.until(ExpectedConditions.elementToBeClickable(By.xpath("(//ul[#class='form']//li)[4]")));
String getText = element.getText();
System.out.println(getText);
You can use the following given below code snippet
WebDriverWait waitforelement = new WebDriverWait(driver, 30);
string elementpath = "//div[#class='box']//child::ul[#class='form']//child::li[4]";
WebElement firstname = wait.until(ExpectedConditions.elementToBeClickable
(By.xpath(elementpath)));
System.out.println(firstname.getText());
I prefer to use the selenium.support package, there's a lot of nice capabilities
it just this easy:
IWebElement selectWebElement = filterColumns.FindElement(By.TagName("select"));
SelectElement select = new SelectElement(selectWebElement);
IList<IWebElement> optionsWebElements = select.Options;
and there's a lot more
Try this:
WebDriverWait Wait = new WebDriverWait(driver, 60);
WebElement element = Wait.until(ExpectedConditions.elementToBeVisible(By.xpath("(//ul[contains(text(),'First Name'])));
String getText = element.getText();
System.out.println("Extracted value is: "+getText);
You can add for loop if you have a common element.

Selenium html button not clicking

This is my HTML code
<button type="button" class="button primary" id="submitID" aria-label="Verify and proceed to next step.">
Verify
</button>
This is my selenium code:
WebElement verifyButton = driver.findElement(By.id("submitID"));
verifyButton.click();
The button comes to focus but not clicking.
Try this in case there are multiple elements with the same id:
WebElement verifyButton = driver.findElement(By.xpath(".//button[#id='submitID']"));
You can use javascript executor . Please ref below code
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('submitID').click();");
You can use any of this thing here
Using ID
WebElement verifyBtn = driver.findElement(By.id("submitID"));
verifyBtn.click();
Using class name
WebElement verifyBtn = driver.findElement(By.className("button primary"));
verifyBtn.click()
Using xpath
WebElement verifyBtn = driver.findElement(By.xpath("//button[#aria-label='Verify and proceed to next step.' and #id='submitID']"));
verifyBtn.click()
Using javascriptExecutor
WebElement verifyBtn = driver.findElement(By.id("submitID"));
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", verifyBtn );
If ID is unique, you might wanna wait for some time and then click on it.
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("submitID"))).click();
OR cssSelector :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[type='button'][id='submitID']"))).click();
OR XPATH :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Verify']"))).click();
OR XPATH :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(#aria-label,'Verify and proceed to next step.')]"))).click();
what is the output of this? note - findElementS
driver.findElements(By.id("submitID")).size()
for sure there are many buttons with same id, which must not happen in valid html

Selenium driver find element not working

Selenium web driver finds element unable to find input field on the web page I tried this each and every option XPath, by CSS, by name. the testing site on the local environment.
HTML:
<input class="form-control ng-pristine ng-untouched ng-valid ng-empty" ng-model="email" placeholder="Username" aria-describedby="username" type="text">
xpath:
/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[2]/div/div/input
css selector:
html.no-js.ng-scope body.pace-done.full-width div#wrapper
div.page-wrapper.white-bg.ng-scope
div.wrapper.wrapper-content.ng-scope div.login-bg.ng-scope
div.container div.row
div.col-sm-6.col-sm-offset-3.col-md-4.col-md-offset-4 div.login-form
form.ng-pristine.ng-valid div.col-sm-12.plr10px div.form-group
div.input-group
input.form-control.ng-pristine.ng-untouched.ng-valid.ng-empty
driver.findElement(By.name("username"))
Here is the Answer to your Question:
As per the HTML you provided, you can use the following xpath to identify the element-
WebElement element = driver.findElement(By.xpath("//input[#ng-model='email' and #placeholder='Username']"));
Incase you are facing an ElementNotVisible exception you can induce ExplicitWait to wait for the element to be clickable as follows:
WebDriverWait wait7 = new WebDriverWait(driver, 10);
WebElement element7 = wait7.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#ng-model='email' and #placeholder='Username']")));
element7.click();
Let me know if this Answers your Question.
Try this, this should work.
WebElement emailInput = new WebDriverWait(driver, 30).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[ng-model='email']")));
JavascriptExecutor je = (JavascriptExecutor) driver;
WebElement Username = driver.findElement(By.xpath("//*[#placeholder='Username' and #type='text'"]));
je.executeScript("arguments[0].scrollIntoView(true);",Username);
Username.sendKeys("Name");