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.
Related
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.
I want to click on drop down button and select one among one and click on it.I tried by many ways but it is not working. Attached image of html code, Anyone help me regarding this.
Below is my code which i tried
driver.findElement(By.id("homepages_dropdown")).click();
/*List<WebElement> allElements = driver.findElements(By.xpath("//*[#id=\"homepages_item_AccountManagerDashboard\"]"));
for (WebElement element: allElements)
{
System.out.println(element.getText());
}*/
Just use "Select" to create an object that handles dropdowns etc. You can use "Select" to select element by it's text, index and a bunch of other options. It's the easiest way to handle such elements as dropdown and comboboxes.
new Select(driver.findElement(By.id("homepages_dropdown"))).selectByVisibleText("Your option's text");
This isn't the sharpest way to do it, but if you can't get developers to make it a specific select element via HTML it may have to do. I have stuff like this in my solution where there are custom dropdowns.
You can start by creating a dropdown element and then click a single record in it if that's what you're trying to do, in your case it looks like it is the span within the highlighted button element, so you can do something like the following:
driver.findElement(By.id("homepages_dropdown")).click();
driver.findElement(By.xpath("//li//a[#id='homepages_item_AccountManagerDashboard']").click();
The above statements will click the dropdown then click the element with the id of homepages_item_AccountManagerDashboard.
You can also create a function that clicks any li of based off of a parameter you pass into a method you create, consider this if you may select many options in your dropdown.
public void SelectItem(string itemText)
{
var dropdownElement = driver.findElement(By.id("homepages_dropdown"));
var selectionItem = driver.findElement(By.xpath("//li[text()='" + itemText + "']");
dropdownElement.Click();
selectionItem.Click();
}
This is only really a valid option if you are working through the Page Object Model, but it's the best way to make things work consistently long term.
To click on Dropdown button and Select the Option with text as Analytics you can use the following code block :
driver.findElement(By.xpath("//div[#id='homepages']/button/span[#id='homepages_dropdown']")).click();
List<WebElement> allElements = driver.findElements(By.xpath("//div[#id='homepages']/ul/li/a"));
for (WebElement element : allElements)
if(element.getAttribute("innerHTML").contains("Analytics"))
{
element.click();
break;
}
System.out.println("Option with text as Analytics is selected");
I found solution myself,Below code worked
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("homepages_dropdown")));
// drop down
driver.findElement(By.id("homepages_dropdown")).click();
Thread.sleep(2000);
// selecting configuration
driver.findElement(By.id("homepages_item_ConfigurationHomepage")).click();
Thread.sleep(2000);
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
Please look into the attached screenshot
I need to select all the data in 1st box at one time.Currently it allows me to select First name and then click on Arrow and then i need to click on Middle name and click on Arrow to move to box 2.I need to do individually for all the data in Box1 to move to Box2.Also i tried drag and drop Its not working Even manually also its not allowing to drag/drop from Box1 to Box2
Can we select all the elements in the box 1 at a time and then click on arrow to move to box2?Please help me out with this issue..
(Note : Below is the Html code for the Box 1)
`
As per provided comment xpath
/html/body/div[4]/div/div[2]/div[2]/div/form/table/tbody/tr[1]/td[1]/select/option
will retrieve list of all options nothing but list of webelements. so use findElements here and collect that list
List<WebElement> elements = driver.findElements(By.xpath("/html/body/div[4]/div/div[2]/div[2]/div/form/table/tbody/tr[1]/td[1]/select/option"));
//use loop here
System.out.println(elements .size());
for(int i=0;i<=elements .size();i++) {
//use click on option
driver.findElement(By.xpath("/html/body/div[4]/div/div[2]/div[2]/div/form/table/tbody/tr[1]/td[1]/select/option")).click(); //it will click first option by default
//write command to click on arrow
//so it will loop upto list size nothing number of options and always select first and clicks on >>
}
thank you,
Murali
You can achieve this solution very easy by Jquery.
the following code very useful to you.
$().ready(function()
{
$('#right_arrow_id').click(function()
{
return
!$('#firstboxid_here option:selected').clone(true).appendTo('#second_box_id_here');
});
for removing options from second box.
$('#left_arrow_id_here').click(function()
{
$('#second_box_id_here option:selected').remove();
});
});
By looking at the first xpath "/html/body/div[4]/div/div[2]/div[2]/div/form/table/tbody/tr[1]/td[1]/select/option[i]", I understood that it is implemented like a dropdown. Hence, you can use "Select" class in selenium. Please find the below code for the same.
// Initializing the Select class
Select names = new Select(driver);
// Retrieving all the options in the dropdown
List<WebElement> allNamesList = names.getOptions();
// Looping through the list
for(WebElement eachName : allNamesList) {
// Clicking on each element in the first box
eachName.click();
// Your Code to click on Arrow button
}
Hope this helps.
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.