WebDriver can't get dropdown menu element (Java) - java

I write a script on Java for Selenium WebDriver, and I have a problem with selected from dropdown menu.
Here's my locator:
new Select(driver.findElement(By.id("FormElement_select_68_input_input"))).selectByVisibleText("Image");
Here's an error: http://prntscr.com/7jul03
Here's HTML code: http://prntscr.com/7jvou6
Need to select "Image" from this menu, but have an error.
Before I had the error like this, I can't upload file, it was because I need to switch to frame(0).
But here I don't know why I can't select menu "Image" from DropBox.

Your ID is dynamic, so you can't use it. Select will not work in your case, you just need to use two clicks
WebElement dropdown = driver.findElement(By.xpath("//div[#class='select-pad-wrapper AttributePlugin']/input"));
dropdown.click();
WebElement element = driver.findElement(By.xpath("//div[#class='select-pad-wrapper AttributePlugin']/div/ul/li[text()='Image']"));
element.click();

It looks like the element id you're looking for"FormElement_select_68_input_input" doesn't exist in your html, your code sample shows "FormElement_select_283_input_container" as the select box element. Try this:
Select droplist = new Select(driver.findElement(By.Id("FormElement_select_283_input_container")));
droplist.selectByVisibleText("image");

Because it is not Select tag.
Try with below logic
WebElement div = driver.findElement(By.cssSelector("div[id*='FormElement_'] > div > div"));
div.click();
WebElement li = div.findElement(By.xpath(".//ul/li[text()='Image']"));
li.click();

As per HTML code screen, i am expecting Select class (selectByVisibleText etc) does not work. can you do one thing, try to click on required option directly. (may be click on "//div[#class='selectbox-wrapper']/ul/li[#class='selectbox_li'][contains(text(),'Image')]" , check one is it correct or not in firepath)
Let me know the result.. if it does not work, as said above you need to click on that input dropdown box and need to click on that Image.
Thank You,
Murali

Related

Select an option from drop down in Java Selenium

I am facing a problem to select an option from the drop down.
The site is https://uk.farnell.com. There is an All drop down just before the Search text box. I want to select "Ceramic Capacitors" from the drop down.
I tried in may ways like using
Select sel = new Select(locator)
JavascriptExecutor
Actions
List
But none worked.
The image displays All has been clicked and Ceramic Capacitors is highlighted which I want to select
xpath for All - .//div[#id='catContainer']
xpath for Ceramic Capacitors - .//option[text()='Ceramic Capacitors']
Select by 3rd index or by visible text.
driver.get('https://uk.farnell.com/')
Select(driver.find_element_by_id('categoryIdBox')).select_by_index(3)
Select(driver.find_element_by_id('categoryIdBox')).select_by_visible_text("Ceramic Capacitors")
Import
from selenium.webdriver.support.select import Select
Please try with below code:
WebElement dropdownlist=Driver.findElement(By.id("categoryIdBox"));
Select listbox = new Select(dropdownlist);
listbox.selectByVisibleText("Ceramic Capacitors");
Can you try the below code it might help to resolve your issue
WebElement element = driver.findElement(By.xpath("((//div[#id='catContainer']//following-sibling::select))"));
Select selectOption = new Select(element);
selectOption.selectByIndex(3);
WebElement option = selectOption.getFirstSelectedOption();
* This is used just to verify the selected option
System.out.println(option.getText());
Please use the below code:
Select options = new Select(driver.findElement(By.xpath("//select[#id='categoryIdBox']")));
options.selectByVisibleText("Ceramic Capacitors");

How use the sendKeys(Keys.TAB) + sendKeys("text") for contact us form

How to fill in the few fields in a column, with commands sendKeys(Keys.TAB) and sendKeys("text").
For Example:
We have 3 fields in Contact Us form at the bottom of the page:
Name
Email
Message
And Submit button.
I have scroll the page to the "Name" field and filled in the "Name", TAB and want to filled in the another text in the next fields (email) by using the sendKeys(Keys.TAB)
That's how I tried it:
WebElement element = driver.findElement(By.cssSelector("#contact > div > div.main > div.form-block"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
WebElement webElement = driver.findElement(By.xpath("//*[#id=\"edit-submitted-name\"]"));
webElement.sendKeys("Name");
webElement.sendKeys(Keys.TAB);
webElement.sendKeys("email");
webElement.sendKeys(Keys.TAB);
webElement.sendKeys("Message");
But it filling in all texts in the first field because webElement moving to first field ("Name").
I know, that I can use driver.FindElement(By.xpath(## next field ##)).sendKeys("text");
But I need exactly to use the TAB button for testing.
Maybe in Java exist the command, to type the text in the focused field? I can't find it.
You have to use Action class of Selenium and create a series of actions to use tab.
Also try all steps manually by clicking tab. sometime tab not work on webpages.

Selenium Webdriver - Stale element exception when clicking on multiple dropdowns while HTML DOM doesn't change

I tried to automate a scenario, where the condition is that I have to select an option from drop down and then there's another dropown next to it, I have to click one option from next drop to enable to button . I tried with the code but it clicks only the first option,.And showing error as stale Element reference:element is not attached to the page document. Please help. Please let me know if in not very clear.
When you select Insurance Test Client then only you get the option Product Insurance, which essentially means the HTML DOM gets changed, which results in StaleElementException. To avoid that, once we select from the first dropdown, we need to induce some wait for the elements of the second dropdown to render in the HTML DOM. Then we will use Select Class to select an option. Try out the following code block:
//Select Channel
Select oSelectChannel = new Select(driver.findElement(By.id("client")));
oSelectChannel.selectByVisibleText("Insurance Test Client");
WebDriverWait wait5 = new WebDriverWait(driver, 10);
wait5.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_a_Category_item")));
//Select Category
Select oSelectCategory = new Select(driver.findElement(By.xpath("//*[#id='category']")));
oSelectCategory.selectByVisibleText("Product Insurance");

Can't Select Checkbox, Selenium WebDriver

I am unable to select a checkbox with Selenium WebDriver in Java.
I tried by Xpath but no result.
WebDriver can't click on element.
I tried with Selenium IDE - recorder, no results.
Here it is - html code for checkbox
I try:
1.
driver.findElement(By.xpath(".//form[#id='placeOrderForm1']/div[#class='terms right']/label")).click();
2.
driver.findElement(By.id("Terms1")).click();
3.
driver.findElement(By.cssSelector("label")).click();
4.
driver.findElement(By.xpath("//div[3]/form/div/input")).click();
Nothing works.
Please help.
Try using JavascriptExecuter Hope this will help
WebElement element = driver.findElement(By.id("Terms1"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", element );
Your code seems correct. Specially this one -
driver.findElement(By.id("Terms1")).click();
It might be possible the element you are clicking is not visible in the page scroll. Try to move to the element first and then click.
Try with this -
WebElement elem = driver.findElement(By.id("Term1"));
Actions action = new Actions(driver).
action.moveToElement(elem).click().build().perform();
Hope this help.
Here is the Answer of your Question:
As you mentioned unable to select a checkbox, actually we don't select the checkbox, we checkmark the checkbox. The checkbox you depicted have an id as Terms1 and name astermsCheck`. So you use either of the locators to checkmark the checkbox as follows:
driver.findElement(By.id("Terms1")).click();
OR
element = driver.findElement(By.name("termsCheck")).click();
Let me know if this Answers your Question.
You can find the element by a unique identifier. In this case, we can use name or id. The better choice is to go with id.
WebElement element = driver.findElement(By.name("termsCheck"));
element.click();
or you can use this one also
driver.findElement(By.id("Terms1")).click();

how to choose a value in dropdown which is not in select tag ..?

I am using selenium-RC for my automation with java (Eclipse Kepler) . I am facing some issues with a selection of a option in a drop down list which is not in select tag it is in span.
I want to test a page where their is a drop-down list for selection of city name. the drop-down appears only when I give some value e.g for "Bangalore" I have to type "ban" so the drop-down appears and then I select the city Bangalore either with "mouse click" or either with "down-arrow and enter key" but when I run my selenium rc script it fails after typing "ban" the drop-down doesn't appears. i tried using both xpath and id in select command,click command. I am stuck over here please please someone help me to solve this issue.I think it is dynamic and based on JavaScript functions.One more ..The next two drop down are depended on the first drop-down(i.e the second and third drop-down are hidden and first drop-down is shown by_default.)
I am sending the link of page where i am trying to test script: https://pizzaonline.dominos.co.in
I tried using following commands:-
1:
w.click("//*[#id='homedeliveryform']/div[1]/span/a/span[1]");
w.click("//*[#id='ui-active-menuitem']");
2:
w.type("//*[#id='homedeliveryform']/div[1]/span/input","ban");
w.click("//*[#id='ui-active-menuitem']");
3:
w.select(" id=combobox", "value=BANGALORE");
4:
w.type(" id=combobox", "value=BANGALORE");
I've had a similar problem. This is how I solved it:
Find the arrow button in the combobox and click it, the dropdown list will appear.
The dropdown list doesn't really have anything to do with the combobox, it is a separate list wich is just displayed in a correct position so that it looks like it belongs to the combobox. The dropdown list is actually an <ul>-element, which doesn't seem to be very easily found. Anyway there are many <li>-elements with <a>-elements in them, and those hold the texts.
As i can see you are trying to select a webelement(Bangalore) from a dropdown which is not visible till the time you click on it. Here are the ways by which you are able to select a hidden web element.
1st way: it is not the problem to click any element using the same js. As you know how to get any option the last actions remaning is to perform a click. This should work for you:
WebElement hiddenWebElement =driver.findElement(By(..selector of the element....));
((JavascriptExecutor)driver).executeScript("arguments[0].click()",hiddenWebElement);
2nd way:
String cssSelector= ...//i gave them in your previous question
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssSelector+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
3rd way: using actions builder, advanced user actions API. You can read about it here And code will be smth like that:
WebElement mnuElement;
WebElement submnuElement;
mnEle = driver.findElement(By.Id("mnEle")).click();
sbEle = driver.findElement(By.Id("sbEle")).click();
Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.moveToElement(mnEle).Perform();
// Giving 5 Secs for submenu to be displayed
Thread.sleep(5000L);
// Clicking on the Hidden SubMenu
driver.findElement(By.Id("sbEle")).click();
Hey thank you so much for the help. I solved this by
w.keyPressNative(String.valueOf(KeyEvent.VK_B));
w.keyPressNative(String.valueOf(KeyEvent.VK_A));
w.keyPressNative(String.valueOf(KeyEvent.VK_N));
w.keyPressNative("40"); // down arrow key
Thread.sleep(4000);
w.keyPressNative("10"); //Enter key
In the below scenario what I did is...(dropdwon is in the table)
clicked the cell
get all cell values from the table(probably the list will be configured as table or list .. here it is table)
traverse through all the cells.
click on the required value based on the cell value.
driver.findElement(By.id("ms__id3")).click();
WebElement table = driver.findElement(By.className("combo-list-table"));
allRows = table.findElements(By.tagName("tr"));
for (WebElement row : allRows)
{
cells = row.findElements(By.tagName("td"));
for (WebElement cell : cells)
{
if(cell.getAttribute("text").equalsIgnoreCase("Business"))
{
cell.click();
}
}
}
Hope this will help...:)

Categories