Hi I tried using the below code to use the dropdown from the foodpanda site and choose the city name but it's not working for me.
public static void main(String args[]) throws InterruptedException{
System.setProperty("webdriver.chrome.driver","C:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.foodpanda.in/restaurants/city/pune?gclid=CIbFi5iEvdMCFdeFaAodujsK5w");
Thread.sleep(5000);
WebElement drp =driver.findElement(By.id("cityId"));
Select drp2 = new Select(drp);
drp2.selectByVisibleText("Bangalore");
It gives out the error-
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible: Element is not currently visible and may not be manipulated
That <select> element has the CSS display: none;. Its just a place holder.
I think the site uses some fancy <span>s to get the awesome look of the drop down. Can you please check that?
So I was trying to figure out how the dropdown works & here's what I found. The select is just a placeholder, it has no role on the webpage. The dropdown is populated using a javascript call when the input box (see below) is clicked.
Once you click inside the input box, some DOM elements are added which contain <p> tag with city names, which when clicked, selects the target city you want to select.
Based on my observation, a span with class name tt-suggestions is visible when the input element is clicked. This span has a div within it which has all the p elements within it. You should aim to click the corresponding p element which has the text of your desired city name.
Here's a working code:
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.findElement(
By.xpath("//input[#class='form-control twitter-typeahead tt-input']")
).click(); // this will click inside the input element
WebElement drp = (WebElement) wait.until(ExpectedConditions.presenceOfElementLocated(
By.xpath("//span[#class='tt-suggestions']"))); // this is the span element which gets populated
System.out.println(drp.getAttribute("innerHTML")); // just in case you want to see the above span's HTML code
drp.findElement(By.xpath("//div/p[text()[contains(.,'Bangalore')]]")).click(); // This will select Bangalore from the Dropdown
Related
I'm building a final project and I got stuck in a problem.
Website: trello.com
I have a page that contains buttons with boards that i have created and button to create a board.
I try to store all these buttons, then locate them by text and click on them.
The problem is this, I manage to access all the buttons, but can not click on them
For example: there are three buttons (see picture), the third button is "create new board".
According to the console, I can import his text, but can not click it (after command click nothing happens).
I'm noob, so I hope I've listed everything, and I'll be happy to help.
This is my code:
There is the pic of the trello page with the boards.
https://ibb.co/kmV6V4n
private By mainBoardSelectorList = By.xpath("//*[#id=\"content\"]/div/div[2]/div/div/div/div/div[2]/div/div/div/ul");
public WebElement getMainBoardSelectorList() {
return driver.findElement(mainBoardSelectorList);
}
#Test
public void getAllBoardsAndClick(){
methodsManager.logIn();
BoardsPage boardsPage = new BoardsPage(driver);
System.out.println(boardsPage.getMainBoardSelectorList().getText());
WebElement el = boardsPage.getMainBoardSelectorList();
if (el.getText().contains("Create new board")){
el.click();
}
}
Jul 11, 2019 12:42:26 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
hj,
Untitled board
Create new board
Double check your selector as it might match something which is not clickable, or clickable but resulting into nothing. Use browser developer tools to find out what are the matches as you might get false positive results
It is possible to locate element by partial link text like:
driver.findElement(By.partialLinkText("Create new board"));
There is findElements() function which returns the List of WebElements which is more suitable for your use case
Be aware that once you click the link and navigate away from the page all the WebElements will be invalidated and you will get StaleElementReferenceException so either consider re-do the "find" once you're back or better go for Page Object Model design pattern which implements lazy initialization tactic when it comes to locating the elements.
You need to click on li tags not ul
You can change your function getMainBoardSelectorList() to return
List<WebElement> instead of WebElement .
public List<WebElement> getMainBoardSelectorList() {
return driver.findElement(mainBoardSelectorList).findElements(By.tagName("li")));
}
Now your method returns all li elements(boards) that you need.
I think "Create new board" is always the last one so you can do
List<WebElement> els = boardsPage.getMainBoardSelectorList();
els.get(els.size() - 1).click());
Don't forget to handle exceptions
Step 1: Navigate and Login
driver.get("https://trello.com/login");
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id='user']")).sendKeys("paste your username here");
driver.findElement(By.xpath("//*[#id='password']")).sendKeys("paste your password here");
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#id='login']")).click();
Thread.sleep(3000);
Step 2: To Click on First Board
Here in xpath #title is your Board name so create xpath according this, for me test is First board name
driver.findElement(By.xpath(".//div[#title='test']/descendant::div")).click();
Thread.sleep(5000);
driver.navigate().back();
Thread.sleep(3000);
Step 3 : To Click on Second Board
Here in xpath #title is your Board name so create xpath according this, for me test1 is Second board name
driver.findElement(By.xpath(".//div[#title='test1']/descendant::div")).click();
Thread.sleep(5000);
driver.navigate().back();
Thread.sleep(3000);
Step 3 : To Click on Third Board
For Click on Create new board manage xpath from class name and click on it.
driver.findElement(By.xpath(".//div[#class='board-tile mod-add']/descendant::span")).click();
by this you can click all your board one by one. You can create function for manage xpath dynamically for board.
I tested first if the first line of code to click the XPath which works but then when I add a second line of code to click By.name() it doesn't work, so I tried to change in XPath and then in CSS selectors but it only clicks the first one the (XPath code of line). I have tried but it doesn't seem to click the two other elements.
What I found out was that it only clicking what was on the first page, didn't really matter what was on the new page and I told to click on an element that I wanted to do. I'm using the Selenium version 3.141.59.
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\ae65255\\Desktop\\java_gui\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://shop.palaceskateboards.com/collections/new");
driver.findElement(By.xpath("//*[#id=\"product-loop\"]/div[#data-alpha='S-LINE JOGGER BLACK']")).click(); //only this one work
driver.findElement(By.name("button")).click(); //second click dosen't work?
driver.findElement(By.linkText("Cart")).click(); //this dosen't work too?
}
Add some wait to let the page load before locating the element
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.name("button")));
button.click();
The third locator By.linkText("Cart") didn't work because the button doesn't have Cart text, it's in the data-text and value attributes.
As a side note, you should use By.partialLinkText() when looking for partial text.
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();
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..:)
I am trying to automate the code of submit resume page, it consists on smart textboxes, which gives suggestions below as soon as you type few text in it. you need to select and input into the textbox from the suggestions given. Below is the code and the url:
WebDriver w= new FirefoxDriver();
w.get("https://www.hrmantra.com/LetsLead/18_Recruitment/SubmittResume.aspx?cn=LetsLead");
w.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
w.findElement(By.id("StCityName_txtSpeedName")).sendKeys("Mumbai");
Only sendkeys command is not working as the entered value has to be selected and the control needs to be closed.
The text box has a select drop down box which appears dynamically on entering the city from it the user selects his city but this select box is inside an iframe (iframe id : SpeedTyperFrameID)so we need to switch to it and then access the select box
Below is the code
WebElement city = driver.findElement(By.xpath("//*[#id='StCityName_txtSpeedName']"));
city.click();
city.sendKeys("chennai");
//wait for the iframe to load and then switch to it
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("SpeedTyperFrameID")));
Thread,sleep(3000);//added just to show u the effect remove it
WebElement byValue = driver.findElement(By.id("SelectList"));
//using select class to select the element by its text
Select select = new Select(byValue);
select.selectByVisibleText("Chennai");
//switch back to default content inorder to access other elements outside the iframe
driver.switchTo().defaultContent();
I have tested the above code it is working fine
Kindly get back if you have any queries.
Ideally, After you type partial text into input you have to find all suggestions from dropdown list and click on it. Perhaps, try to use enter key, but I am not aware that it will help
element.sendKeys("Mumbai" + Keys.ENTER)
It's because there is no ID of id=StCityName_txtSpeedName on the page. You don't use id=... for the ID, you just type the ID.
See below.
w.findElement(By.id("StCityName_txtSpeedName")).sendKeys("Mumbai");