Selenium Java - How to Select a Dropdown Element that Has No ID - java

I am using Selenium - Java WebDriver (ChromeDriver) as a new user.
While trying to select an item from a dropdown menu using Java, I couldn't do it because of the error message. Please note that I've tried variations of Select and WebElement options but not having the expected result: clicking the link from the drop down menu that should take me to the target page.
Here is the error message shown in eclipse:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"Payment"}
Here is the relevant code segment:
Select dropdown2 = new Select(webDriver.findElement(By.linkText("Payment")));
dropdown2.selectByVisibleText("Payment");
I have also tried the following with no success:
WebElement element = webDriver.findElement(By.cssSelector("a[class='glyphicon glyphicon-credit-card']"));
element.click();
Also, the following code that didn't work:
WebElement element = webDriver.findElement(By.partialLinkText("Payment"));
Select mySelect= new Select(element);
mySelect.selectByVisibleText("Payment");
The segment of html is shown below:
Hoping to get feedback.
Thank you.

As I'm seeing in provided screenshot HTML, this is not a <select> element, so you can't use Select() class here to work with dropdown.
You should try using simple finder with WebDriverWait as below :-
WebDriverWait wait = new WebDriverWait(driver,10);
//First click on dropdown down to open options
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("li.dropdown > a.dropdown-toggle"))).click();
//Now select opened option
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("ul.dropdown-menu > li > a[href*='Billing']"))).click();
Or
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Payment"))).click();
Or
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Payment"))).click();

Related

Silenium webdriver using Java: Exception in thread "main" org.openqa.selenium.ElementNotVisibleException:

I am using Webdriver and trying to click on a link (Billing) which further has a dropdown(My Quotes). To locate billing and then to click on My Quotes link, I am using the below code:
String xp = "//*[#id='Primary_Navbar-Billing']/a"; // With this xpath I can search on my Firefox browser but using the same in my code gives me an error:
WebElement menu = driver.findElement(By.xpath(xp));
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
// Initiate mouse action using Actions class
Actions builder = new Actions(driver);
// move the mouse to the earlier identified menu option
builder.moveToElement(menu).build().perform();
//identify menu option from the resulting menu display and click
driver.findElement(By.linkText("My Quotes")).click();
I am getting an error:
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException:
you can try those 3 options:
Add a Thread.sleep(YourMilliSecondesTime); before your click(); action.
Use Xpath instead By.linkText to find your link, Xpath is ALWAYS the better option.
or try this: driver.findElement(By.xpath("//span[text()='YOURLINKTEXTEHERE']")).click()); if for any reason you don't have a good xpath for this element.
Hope this will help. :)

Selenium getting text input of Twingly (Java Code)

Please check out the element of this website.
It has a form, and along with 2 text input and 1 submit button.
I dont know which one from those 2 inputs that is actually used when the user type-in some urls over there.
But when I tried this (using firefoxDriver) to get the element:
WebElement textfieldURL = driver.findElement(By.id("ping-url")); // even ping-box not working
The result's unable to locate the element.
Then I change my code to this :
driver.switchTo().frame(driver.findElement(By.className("ping-iframe")));
WebElement textfieldURL = driver.findElement(By.id("ping-url")); // even ping-box not working
The result's still unable to locate the element.
Any clues?
You haven't mentioned the exception which you are facing. As your input tag present under iframe so you need to first switch into frame and than have to perform actions -
driver.switchTo().frame(driver.findElement(By.className("ping-iframe")));
//or you can use frame index as well
driver.switchTo().frame(0);
your element is available with the id ping-box . Try the following complete code -
System.setProperty("webdriver.gecko.driver","D:/Application/geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.twingly.com/ping");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.switchTo().frame(driver.findElement(By.className("ping-iframe")));
driver.findElement(By.id("ping-box")).sendKeys("http://www.google.com");
driver.findElement(By.id("ping-button")).click();
Same is working for me.

How to locate Telerik controls in Selenium?

URL :
http://demos.telerik.com/aspnet-ajax/dropdownlist/examples/overview/defaultcs.aspx
Question :
How to select dropdown value?
My Code:
Select dropdown = new Select(driver.findElement(By.xpath("//a[#class='rddlSlide']//span")));
dropdown.selectByVisibleText("Chai");
How to select dropdown value?
Actually target element is not proper <select> element, Hence you can't handle is using Select class.
Try as below :-
driver.get("http://demos.telerik.com/aspnet-ajax/dropdownlist/examples/overview/defaultcs.aspx");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 60);
//This line would find the dropdown element and open the options
wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_ContentPlaceholder1_RadDropDownProducts"))).click();
//This line would select the desire option using their text
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[text() = 'Chai']"))).click();

need help to find Xpath of following html--selenium--

I am using firebug for finding xpath, this is the error that is displayed in the error console.
no such element: Unable to locate
element:{"method":"xpath","selector":".//*[#id='select2-contact_id-result-v0w5-258']"}
driver.findElement(By.xpath(".//*[#id='select2-contact_id-result-v0w5-258']")).click();
html is as follow
id="select2-contact_id-result-v0w5-258" class="select2-results__option select2-results__option--highlighted" role="treeitem" aria-selected="false">Single contact
There could be following reason for it :-
May be your id is dynamically generated, so you need to try with different locator to create By object as below
By by = By.className("select2-results__option select2-results__option--highlighted");
or
By by = By.cssSelector(".select2-results__option select2-results__option--highlighted");
or
By by = By.xpath("//*[contains(., 'Single contact')]");
or if id is not dynamically generated
By by = By.id("select2-contact_id-result-v0w5-258");
May be your element is inside a frame or iframe, If it is then you need to switch that frame or iframe before finding element as below :-
driver.switchTo().frame("frame id or name or index");
May be element is not being fully loaded on the page due to slow internet, so you need to implement WebDriverWait to wait until element is visible and clickable as below :-
WebDriverWait wait = new WebDriverWait (driver, 10);
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(by)); //use anyone of the above by object
Now after successfully getting element you need to perform click as below :-
el.click();
Note :- if your element is actually a dropdown with select tag, then you need to create Select() object to work with dropdown as below :-
Select sel = new Select(el);
//now perform step to select an option by visible text from dropdown
sel.selectByVisibleText("your visible option text");
or
sel.selectByIndex("your option index");
or
sel.selectByValue("your option value");
Hope it helps you..:)

unable to locate href element within a mouse over area - Selenium web driver

I tried to locate a link "Sign in" located in the window which appeared when mouse move on to "Mail" link on Yahoo. I can get the xpath using firebug. but when i used it in the script, it doesn't work.
HTML snippet :
<a id="yui_3_18_0_4_1456816269995_943" class="C($menuLink) Fw(b) Td(n)"
data-ylk="t3:usr;elm:btn;elmt:lgn;" data-action-outcome="lgn"
href="login.yahoo.com/config/…; data-rapid_p="23">Sign in</a>
I tried this in my code within main method,
WebElement element = driver.findElement(By.id("uh-mail-link"));
Actions action = new Actions(driver);
action.moveToElement(element).build().perform();
driver.findElement(By.id("yui_3_18_0_4_1456804882382_929")).click();
id selecter;
driver.findElement(By.id("yui_3_18_0_4_1456804882382_929")).click();
It prompts this error;
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"yui_3_18_0_4_1456804882382_929"}
Command duration or timeout: 17 milliseconds
Can we locate it using id of appeared window ".//*[#id='yui_3_18_0_4_1456804882382_919']" and linkText "Sign in", or are there any other methods to locate it in the script.
You're supposed to pass just id to By.id(), not an XPath expression :
driver.findElement(By.id("yui_3_18_0_4_1456804882382_929")).click();
or use By.xpath() instead of By.id() if you need to find the element by XPath expression, for example, by using combination of id and link text to locate the target element.
UPDATE :
You can filter element by its text content and id like so :
//a[#id='yui_3_18_0_4_1456816269995_943' and .='Sign in']
Have you tried putting 2 functions ? 1 for mouse over and 1 for actual clicking ?
I have a sample in Java if you need.
public void mouseOver(String xPath){
Actions action = new Actions(driver);
WebElement element = driver.findElement(By.xpath(xPath));
action.moveToElement(element).moveToElement(driver.findElement(By.xpath(xPath))).click().build().perform();
Thread.sleep(500); //too actualy see if is it performs
}
public void click(String xpath) {
driver.findElement(By.xpath(xpath)).click();
}
You can change the searching method from xpath to id, or whatever you need.
Be aware that the mouseOver function has a different xpath/id to send from the click method because , you mouseOver on first button, and you click the other link that will apear after.
Hope it helps.

Categories

Resources