I'm trying to select a value from a dropdown list but I'm getting an ElementNotVisibleException error.
new SelectElement(driver.FindElement(By.CssSelector(select.selectText[name=ADT]))).SelectByValue("2");
And you can see the website that I try to select from dropdown list is
Css of the website
I believe there something to do with jQuery as well but I'm not sure..
Thanks.
You can try something with JavaScript. For example, if you have a hidden button, you can click that button in this way:
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", driver.FindElement(By.Id("loginLink")));
See if you can apply the same thing for your dropdown element.
Hope it helps.
Don't you just miss a quotation mark there?
css=selector.class[attribute='value']
In your case:
select.selectText[name='ADT']
or ""
Please try this :
new Select(driver.FindElement(By.CssSelector(select.selectText[name='ADT']))).SelectByValue("2");
You'll need to wait until the element is visible using an explicit wait.
wait.until(ExpectedConditions.visibilityOfElement(By.cssSelector("select.selectText[name=ADT]")
Related
enter image description here
I need to click the 'Practice Form' tag highlighted in the attached image using C# Selenium here.
The url of form is https://demoqa.com/forms
and the upon clicking 'Practice Form' it will be redirected to https://demoqa.com/automation-practice-form.
I need to achieve this through clicking, not by navigating.
This XPATH should perfectly work:
//span[contains(.,'Practice Form')]//ancestor::li[#class='btn btn-light ']
Short explanation:
You find span containing specified text
You go to ancestor to get button's xpath.
You need to find it with selenium and use Selenium's click()
This is one of xpaths you can use.
Another option is just going a level up:
//span[contains(.,'Practice Form')]/..
I like both.
Option 3:
//span[contains(.,'Practice Form')]//parent::li[contains(#class,'btn btn-light')]
I am not sure why yours isn't working, but this works fine for me:
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.demoqa.com/forms");
driver.FindElement(By.XPath("//span[contains(.,'Practice Form')]//parent::li[#id='item-0']")).Click();
It takes me to:
https://www.demoqa.com/automation-practice-form
With the Student Registration form shown in the middle
BTW in your original comments you said you had to use XPath for this because that field is "dynamic". It is not dynamic. The element has more than one ID: item-0, maybe that is what you meant by couldn't use ID. But the element itself is not "dynamic"
This simple XPath works for me
//span[text()='Practice Form']
You may use the below xpath
//li[#class[contains(.,'btn-light')]][contains(.,'Practice Form')]
I am very new to Selenium. On our webpage developers have used jQuery chosen select to fill dropdown. What I want to do is pass specific text and select matching text I entered.
So I tried this:
[FindsBy(How = How.XPath, Using = "//div[#id=MyDrpdown_chosen]//div[#class='chosen-drop']//div[contains(#class,'chosen-search')]/input"), CacheLookup]
private HtmlElement _selectItem;
_selectItem.SendKeys("Banana");
Update 1
Here is screenshot of source inspection in debugger tool
But I get error that it couldn't find matching element. Can someone guide me?
//div[#id=MyDropdown_chosen]
has to be
//div[#id='MyDropdown_chosen']
(you are missing the single quote)
UPDATE 1: Just change your dropdown id
like this : MyDrpdown_chosen
[FindsBy(How = How.XPath, Using = "//div[#id=MyDrpdown_chosen]//div[#class='chosen-drop']//div[contains(#class,'chosen-search')]/input"), CacheLookup]
private HtmlElement _selectItem;
_selectItem.SendKeys("Banana");
If your code is faster than the results are updating you can have a problem finding or interacting with your element. You should make sure you wait enough time for the list to update.
Also note the typo and the missing quotes around MyDrpdown_chosen.
I'm trying to automate the drop down field value using c# selenium. I use the following code snippet for select the drop down value and as well as the option.
Code
But I'm not able to click that option .If I trying to write a click functionality for the selected option I'm getting error.
Cannot click on option element.Executing javascript function returned an un expected error,but no error could be returned from IE's
javascript Enginge. .
Please refer the following Screen shot for the Error reference .
Error reference
How can I change the value in dropdown using selenium c# ?
I'm not familiar with C# but this is how you'd do it in java. I believe the syntax is similar so maybe someone can translate it or something?
List<WebElement> dropdown = driver.findElements(By.cssSelector("Your css/xpath/whatever here");
for (int i =0;i<dropdown.size();i++){
WebElement ele = dropdown.get(i);
String textOfElementYouWantToClick = ele.getText();
if(textOfElementYouWantToClick.contains("Value you are looking for")){
ele.click();
System.out.println("Selecting the value you chose");
}
}
Try using the SelectElement class. It has methods for handling dropdown fields.
https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_SelectElement.htm
Have you tried not using javascript to click selected option?
There is Select selenium class to handle dropdowns:
Select myDropdown = new Select(dropdownWebElement);
myDropdown.selectByVisibleText('Value to select');
The other reason for it might be the dropdown is rendered with some framework that keeps actual Select hidden and what you see on screen is actually items from very different element in your DOM.
I'm making an application that can find an item on a website if the item name and colour matches the one set inside the application.
If the item name is set as "backpack" and colour set as "green" the application should find a match on the page and click the link. The website is this: Click
I would prefer doing this in C# with http requests or something similar. I would also do PhantomJS if anyone has a better solution using it.
You can use selenium, it basically allows you to act like a user with actual web browser. http://www.seleniumhq.org/
You can do something like mentioned below with the help of XPath:
driver.Navigate().GoToUrl("http://www.supremenewyork.com/shop/all/bags");
var backpack= driver.FindElement(By.XPath("//*[contains(#class,'inner-article')]//h1//a[contains(., 'Backpack') or contains(., 'Backpack')]"));
var colorGreen = driver.FindElement(By.XPath("//*[contains(#class,'inner-article')]//p//a[contains(., 'Acid Green') or contains(., 'Acid Green')]"));
if (backpack.Text == "Backpack" && colorGreen.Text == "Acid Green")
colorGreen.Click();
It is a tested code, it successfully finds the required values within tags, clicks and moves to that page.
Hope it helps.
I have a iframe modal under my site. I am trying to click button in
it but I am unable to do so. Below is my code. Please let me know
what am i missing
driver.SwitchTo().Frame(driver.FindElement(By.Id("iframeid='frame_name'")));
driver.FindElement(By.Id("sendReuqest")).Click();
Expected Result: Button id: sendRequest should get clicked which is on iframe
Actual Result: Element is not found.
Please let me know if you have any questions.
Try to do it this way. Let's take frame_name id as iframe_1. Whatever you frame id is you can add instead of iframe_1. Also you have a spelling mistake (typo) it might be sendRequest so I am adding as id of your button.
driver.SwitchTo().Frame(driver.FindElement("iframe_1")));
driver.FindElement(By.Id("sendRequest")).Click();
Hope it works. Please do comment and let us know.
Best of luck.
It looks like you're trying to use By.Id() when you should be using By.CssSelector(). By.Id() expects you to pass a parameter which matches the ID element in the HTML.
driver.SwitchTo().Frame(driver.FindElement(By.CssSelector("[iframeid='frame_name']")));
driver.FindElement(By.Id("sendReuqest")).Click();
Try that one:
driver.SwitchTo().Frame("frame_name");
driver.FindElement(By.Id("sendReuqest")).Click();