Find items in jquery chosen in selenium - c#

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.

Related

When I try to use XPath to click a button using C# Selenium it doesn't work

I tried to click on a button by using the buttons XPath in C# Selenium. But in C# it shows the below error. I don't know what to do I am new this platform. So kindly help me with this.
Nothing but I just removed the double quotes. Instead I used the single quotes for [#id='content'].
IWebElement search = driver.FindElement(By.XPath("//*[#id='content']/div/form/div[2]/div[4]/a"));
search.Click();

C# Selenium XPath , call click function of a link text

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')]

Select option set values in Selenium Webdriver

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.

Selenium C#: Not able to identify xpath with class name

I wanted to click on the highlighted record on result set window. I used the xpath as
""//div[contains(#class, 'email-icon icon')]/label[contains(#class, 'text')]";"
But,script is not able identify the record.
Note:Div id is dynamic.
Element: Record marked with blue color in screen shot.
Xpath used: "//div[contains(#class, 'email-icon icon')]/label[contains(#class, 'text')]";
HTML code:
**Dhanaprabhu0106**
It seems Webdriver was not able to reach to the required label using xpath you had used. Try following:
//div[starts-with(#id, 'sc')]//div[#class='sc-view sc-table-row-view sc-collection-item even hover sc-regular-size']/div/img[#class='email-icon icon']/following-sibling::label[normalize-space(text())='Dhanaprabhu0106']
Let me know, whether it works for you.
Update 1:
Try to click using IJavaScriptExecutor, as shown below:
IJavaScriptExecutor e = (IJavaScriptExecutor)driver;
e.ExecuteScript("arguments[0].click();", driver.FindElement(By.xpath("//div[starts-with(#id, 'sc')]//div[#class='sc-view sc-table-row-view sc-collection-item even hover sc-regular-size']/div/img[#class='email-icon icon']/following-sibling::label[normalize-space(text())='Dhanaprabhu0106']"));
Let me know, whether it works for you.
Your XPath is incorrect. See my examples below.
//div[contains(#class, 'has-icon')]/label[contains(#class, 'text')]"
or
//img[contains(#class, 'email-icon')]/../label[contains(#class, 'text')]"

WebElement (Radio Button List) cannot be found in Internet Explorer

I have a WebElement in which cannot be found in IE 10 but works in FF 47.1 and Chrome 51.
[FindsBy(How = How.XPath, Using = "Foo's XPath")]
IWebElement Foo = null;
At runtime I have also called
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("Foo's XPath")));
But am receiving a NullReferenceException for IE10 Only. I have even tried a sleep as well. Can someone please advise me what else I can do b/c my program has to run IE.
As of the time this question was asked we have upgraded to IE 11 but am still seeing the exact same issue. I can't provide specific html code but I want to elaborate a little on the webelement foo.
The radio button which I have called "foo" is wrapped in the following
div
table
tbody
tr
td
input id = "foo"
When I call the xpath (I have tried id and css selector as well)
.//*[#id='foo_rblArgType_4']
the application itself defaults to the 1st value of the radio buttons and I can't seem to get it to select the 4th value which I need. Can anyone offer advice or suggestions of something else I can try?
Depending of what elements are siblings you can have selectors like:
//tr[4]//*[contains(#id, 'foo')]
//td[4]/*[contains(#id, 'foo')]
//*[contains(#id, 'foo')][4]
You can also use the entire id if is not changing by replacing [contains(#id, 'foo')] with [#id='foo']
If none of this are working then please provide the structure for all 4 inputs.
Try identifying element by id.
[FindsBy(How = How.Id, Using = "foo")]
IWebElement Foo = null;
Also make sure that the element is not hidden. Also try increasing the size of the element as sometimes web-driver is not able to locate element.
If you still have issues performing any operations on radio button, try using JavaScriptExecutor.
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html

Categories

Resources