Purpose: My code should get the all the links that are available under a drop down menu. It should print all those links under that specific menu.
Site used for testing: http://test1.absofttrainings.com and the specific menu option is Test Pages.
Question/Problem: The code is not printing 2 values that I am expecting, there are 2 links under Test Pages.
Code:
WebDriver driver= new FirefoxDriver();
driver.get("http://test1.absofttrainings.com/#");
//Step1: Create a List of WebElements to put all the links
// the //a will give me all the links associated
List<WebElement> drop_downs= driver.findElements(By.xpath("//a[contains(text(), 'Test Pages')]//a"));
for(int i=0;i<drop_downs.size(); i++){
WebElement e= drop_downs.get(i);
String text=e.getAttribute("innerHTML");
System.out.println("Links are " + text);
}
Thanks in advance for your time.
Code snippet
//find all the links of sibling(s) of the 'Test Pages'
List<WebElement> drop_downs= driver.findElements(By.xpath("//*[contains(text(), 'Test Pages')]/following-sibling::*//a"));
for(WebElement links : drop_downs){
System.out.println(links.getAttribute("innerHTML"));
}
Related
I intend to automate a simple form fill Junit test on the following website: https://newdesign.millionandup.com
The input textfield i am attempting to send keys to is identified with id=email and i have tried the following two methods:
1.
WebElement passInput = driver3.findElements(By.className("sidebar__item-text")).get(0); // this xPath does the trick
wait.until(ExpectedConditions.visibilityOfElementLocated(emailLocator));
passInput.click();
passInput.sendKeys("emailID");
Thread.sleep(3000); // only to see the result
WebElement em1 = driver3.findElement(emailLocator);
JavascriptExecutor jse = (JavascriptExecutor)driver3;
//driver3.findElement(emailLocator).sendKeys(emString);
Thread.sleep(1000);
jse.executeScript("arguments[0].click()", em1);
jse.executeScript("arguments[0].value='prisoner24601#gouvernement.fr';", em1);
if (em1.isSelected()) {
System.out.println("email typed");
} else {
System.out.println("unable to send keys, element not interactable");
}
To no avail in both cases, the textfield is still not interactable and i am unable to type the data into it.
EDIT: As suggested in the comments i have attempted a third way:
3.
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[#id='email']")));
System.out.println("email input located");
element.click();
System.out.println("email input clicked");
element.sendKeys(emString);
Element is actually located, but, as soon as the methods click() or sendKeys() are called, the execution stops after stating "element not interactable"
The xpath - //input[#id='email'] is matching 9 elements in the DOM. Its important to find unique locators - Link to refer
And the pop-up for Register opens on mouse movement. After mouse movement, will be able to view and interact with the pop-up elements.
Used below xpath to send keys to the element Email Address and it worked for me.
//form[#id='frmLeadModal']//input[#id='email']
Try like below once, it might work for you too.
driver.get("https://newdesign.millionandup.com/#initial");
WebDriverWait wait = new WebDriverWait(driver, 30);
Actions actions = new Actions(driver);
actions.moveByOffset(100, 100).perform();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//form[#id='frmLeadModal']//input[#id='email']"))).sendKeys("example#email.com");
I am trying to get correct page number for Seattle which is 7 and then click on every pages from 1 to 7. I have 2 problems see the parts with asterisk ** below:
1) I am not sure how to write the xpath so that I get 7 instead of 11
2) The while loop is an infinite loop
public class Job {
public static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Shahriar\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver= new ChromeDriver();
driver.get("https://web.expeditors.com/careers/#/listTop");
driver.manage().window().maximize();
//Disable the cookie notification at bottom
driver.findElement(By.xpath("//a[#id='hs-eu-decline-button']")).click();
// Find more cities and scroll down
driver.findElement(By.xpath("//a[#id='moreCity']")).click();
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,5500)", "");
//Locate US-Seattle and click and verify
driver.findElement(By.xpath("//label[#class='ng-binding']//input[#id='us~Seattle']")).click();
String state=driver.findElement(By.xpath("//a[contains(text(),'United States - Seattle')]")).getText();
Assert.assertEquals(state, "United States - Seattle");
**//Number of search pages found should be 7
List<WebElement> resultPages=driver.findElements(By.xpath("//a[#href='#listTop']"));
Assert.assertEquals(7, resultPages.size());**
**//Go to each page by clicking on page number at bottom
int count=0;
WebElement next_button=driver.findElement(By.xpath("//a[#ng-click='setCurrent(pagination.current + 1)']"));
while (resultPages.size()!=0){
next_button.click();
count++;
System.out.println("Current page is "+ count);
}**
//driver.close();
}
}
I could use some help. Thanks in advance for your time.
Here is the logic that you need.
//Number of search pages found should be 7
List<WebElement> resultPages=driver.findElements(By.xpath("//a[#href='#listTop' and #ng-click='setCurrent(pageNumber)']"));
Assert.assertEquals(7, resultPages.size());
for (int i = 1; i <= resultPages.size(); i++) {
driver.findElement(By.xpath("(//a[#href='#listTop' and #ng-click='setCurrent(pageNumber)'])[" + i +"]")).click();
System.out.println(driver.findElement(By.xpath("(//a[#href='#listTop' and #ng-click='setCurrent(pageNumber)'])[" + i +"]")).getText());
}
Your while loop is counting pages, but the condition itself appears to be static (I can't tell without the actual page). You probably want something more like
int count=0;
WebElement next_button=driver.findElement(By.xpath("//a[#ng-click='setCurrent(pagination.current + 1)']"));
while (next_button != null){
next_button.click();
count++;
System.out.println("Current page is "+ count);
next_button=driver.findElement(By.xpath("//a[#ng-click='setCurrent(pagination.current + 1)']"));
}
As for your 7 vs 11 result pages, again it's hard to tell without the HTML, but your XPath search is for By.xpath("//a[#href='#listTop']") so I'd check and see what's in your HTML with listTop. Perhaps other elements are somehow using that anchor tag?
I am trying to implement some code using Selenium Webdriver using Java.
Basically, I have a website with a text box. Once user enter the first letter, based on that a value will be displayed(using AJAX). I need to select the particular value, which i mentioned in send keys .
WebElement fromCity = driver.findElement(By.id("pickUpLocation"));
fromCity.sendKeys("A Ma Temple / 媽閣");
Thread.sleep(2000);
WebElement ajaxContainer1 = driver.findElement(By.className("txt-box ng-touched ng-dirty ng-valid"));
WebElement ajaxHolder1 = ajaxContainer1.findElement(By.tagName("ul"));
List<WebElement> ajaxValues1 = ajaxHolder1.findElements(By.tagName("li"));
for (WebElement value1 : ajaxValues1) {
if (value1.getText().equals("A Ma Temple ")) {
((WebElement)ajaxValues1).click();
break;
}
}
After you send keys.your Ajax value should be retrieved in a box related to you keyword search.You need to get the complete box.and fetch each one as you have done in for loop .get the text and compare it with your expected text and click where this condition is true.
What is that line for before thread.sleep()
I think u can try selecting through index. It should be like this
List<WebElement> ajaxValues1 = ajaxHolder1.findElements(By.tagName("li"));
Select dropdown= new Select(ajaxValues1);
dropdown.selectByIndex(0);
dropdown.selectByIndex(1);
dropdown.selectByIndex(2);
0 represents the first element in the dropdown. Based on index number of that element, feed the corresponding number in selectByIndex(0)
Let me know if this helps. Thanks
I am new to Selenium and i am stuck in one implementation
My requirements are
Go to Settings page
Look for the dropdown
Fetch all values from dropdown
Look for a certain text in dropdown
If the text is not selected,Select the particular text in dropdown
My code is as follows
void setru()
{
driver.switchTo().frame("contentFrame");
Select rudropdown=new Select(driver.findElement(By.id("DefaultOrganisationDropDown")));
List<WebElement> drop=rudropdown.getOptions();
int e=drop.size();
for(int i=0;i<e;i++)
{
String temp=drop.get(i).getText();
String actual_RU="000139, NEXTRAN CORPORATION - JACKSONVILLE";
boolean flag=false;
if(temp.contains("000139"))
{
flag=true;
rudropdown.selectByValue("actual_RU");
}
}
But this is giving me error message.
org.openqa.selenium.NoSuchElementException: Cannot locate option with value: actual_RU
It may works if you replace the below line in your code
rudropdown.selectByValue("actual_RU");
with
rudropdown.selectByValue(temp);
There seems to be a typo mistake. Use below code
rudropdown.selectByValue(actual_RU);
Instead of
rudropdown.selectByValue("actual_RU");
I'm trying to make a small java app. that can log in to my university portal, I used Selenium in the following code:
//some import statements
public class Portal{
public Portal(){
File file = new File("C:/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
HtmlUnitDriver driver = new HtmlUnitDriver();
String target = "http://portal.kfupm.edu.sa/cp/home/loginf";
driver.get(target);
ArrayList <WebElement> inputs = (ArrayList<WebElement>) driver.findElements(By.tagName("input"));
System.out.println(inputs.size());
for(WebElement input : inputs){
System.out.println(input.getAttribute("value") + " " + input.getAttribute("name"));
// to insure the link is displaying something
ChromeDriver driver1 = new ChromeDriver();
driver1.get(driver.getCurrentUrl());
}
public static void main(String [] args){
new Portal();
}
}
the problem is when I use this target (my university portal) I get inputs.size() = 0; although, I'm sure there are elements with (input) tagName. Also I get the same resulst whatever was the method of (By) class I used.
However, when I change the target to any link (for example: "http://www.google.com" or "http://www.facebook.com" , I get elements in the inputs ArrayList (all elements of tagName (input) that are in the target html page). Can any body please tell me what is the problem and how can I solve it? thanks in advance..
The reason that you find zero elements with tag input is that all the elements lie inside a frame tag. Selenium is able to see to current window and all the elements that are defined inside frame tag (i.e. are inside a frameset) are invisile to it.
To see the element you will need to switch frame first, So that selenium has control inside the target frame and not in the outer window. Try this
driver.swtichTo().frame(0); // this will move selenium control inside first frame