Selenium WebDriver -- XPath - java

Trying to get the script to select the filter button on the top but can't seem to figure out how to input the XPath. I believe it has something to do with it be in a separate iframe.
package chromebrowser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class JavaClass {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Newfolder\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://mlvtwrew73.consilio.com/Relativity/");
driver.manage().window().maximize();
//Thread.sleep(5000); this can be used as a wait command before moving on to the next function
WebElement objWE;
Thread.sleep(9000);
// objWE = driver.findElement(By.linkText("User Status"));
// objWE.click();
driver.switchTo().defaultContent();
driver.findElement(By.xpath("id(\"ctl00_ctl00_itemList_FilterSwitch\")")).click();
// objWE = driver.findElement(By.id("1"));
// driver.close(); will be used to close the site once all testing completes
}
}

Use ID locator -- it's more appropriate here (and faster than XPath):
WebDriverWait wait= new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.id("ct‌l00_ctl00_itemList_F‌​ilterSwitch")));
driver.findElement(By.id("ctl00_ctl00_itemList_FilterSwitch")).click();

Looks like you're passing in an incorrect XPath. Try this one:
driver.findElement(By.xpath("//a[#id='ctl00_ctl00_itemList_FilterSwitch']")).click();

If there is then why you are using xpath.
Use id like this
driver.findElment(By.id("ctl00_ctl00_itemList_FilterSwitch"). click ();

I need more clarity regarding your question.
As I have understand, I think you need a requirement of XPath of the Filters image. If i am right, try this:
//div[#class='actionCellContainer']//a/img[#class='itemListActionImage']

Related

How to perform multiple actions and click on the link with text as Member Login on the url http://www.spicejet.com/ through selenium-webdriver

I tried the below code but it is not mouse hovering and clicking on 'Member login'
WebElement lgn = driver.findElement(By.id("ctl00_HyperLinkLogin"));
WebElement ssm = driver.findElement(By.xpath("//a[contains(text(), 'SpiceCash/SpiceClub Members')]"));
WebElement cgm = driver.findElement(By.xpath("//a[contains(text(),'Member Login')]"));
Actions a1 = new Actions(driver);
a1.moveToElement(lgn).moveToElement(ssm).moveToElement(cgm).click().build().perform();
To invoke click() on the element with text as Member login, first you have to Mouse Hover over the element with text as LOGIN / SIGNUP, then Mouse Hover over the element with text as SpiceCash/SpiceClub Members then induce WebDriverWait for the element with text as Member Login to be clickable and you can use the following solution:
Code Block:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Spicejet_member_login {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.spicejet.com/");
new Actions(driver).moveToElement(new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.link#ctl00_HyperLinkLogin")))).build().perform();
new Actions(driver).moveToElement(new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[#class='hide-mobile']/a[contains(.,'SpiceCash/SpiceClub Members')]")))).build().perform();
new WebDriverWait(driver, 7).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[#class='hide-mobile']//ul/li/a[#href='https://book.spicejet.com/Login.aspx' and contains(.,'Member Login')]"))).click();
}
}
Browser Snapshot:
You can try to add waits between your moveToElement() calls
WebDriverWait wait = new WebDriverWait(WebDriverRunner.getWebDriver(), 10);
wait.until(ExpectedConditions.visibilityOf(element))
where the "element" is your menu that should appear on hover.
Or you can use ready solution Selenide framework which is built on top of the Selenium and has built in hover method and waits which help to handle page dynamics
By this link you can find an example of hover() method usage.

Why does Selenium testing website not work on more than one page?

The first part of the code work will work. But the second part doesn't work, and no error appears, and I don't know where the problem is. So please help.
First part is the login page, and the second part is home page.
package Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
public class test {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
System.setProperty("webdriver.gecko.driver",
"E:\\Quality\\drivers\\geckodriver.exe");
driver =new FirefoxDriver();
driver.get("https://www.linkedin.com/uas/login");
// first part//
driver.findElement(By.xpath("//*[#id=\"session_key-login\"]")).click();
driver.findElement(By.xpath("//[#id=\"session_keylogin\"]")).sendKeys("Email");
driver.findElement(By.xpath("//*[#id=\"session_password-login\"]")).click();
driver.findElement(By.xpath("//*[#id=\"session_password-login\"]")).sendKeys("*******");
driver.findElement(By.xpath("//*[#id=\"btn-primary\"]")).click();
// second part//
WebElement test = null ;
test.findElement(By.xpath("/html/body/div[5]/div[5]/aside/div/header")).click();
}
}
You have putted a wrong id for xpath of email textbox.
You should use session_key-login instead of session_keylogin.
Just use the below revised code where you're using sendKeys() method:
driver.findElement(By.xpath("//*[#id=\"session_key-login\"]")).sendKeys("Email");
2nd part Solution
Skip WebElement declaration, thus comment the line //WebElement test = null;
Use the line using driver object
driver.findElement(By.xpath("/html/body/div[5]/div[5]/aside/div/header")).click();
You can also use xpath //*[#id=\"msg-overlay\"]/div/header
thus for the revised code is:
driver.findElement(By.xpath("//*[#id=\"msg-overlay\"]/div/header")).click();

How to set focus on element on moving the mouse over it in Selenium WebDriver using Java?

I am a beginner in Selenium WebDriver and I'm using this test on the StackOverflow homepage. These will be the steps of my test:
Firstly, go to the homepage of the StackOverflow.
Then, move the mouse on the Users button such that it will be focused. (We don't have to click on it just hover the mouse over it.)
Now, find the active element on the screen (i.e. the element which is
currently focused) and then click on it.
I want the Users button to be clicked because it is focussed currently but that's not happening. Instead the Questions button is clicked on.
Here is my code for the same test.
package insertCartridge;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Practice {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "D:\\SELENIUM\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
String baseUrl = "https://stackoverflow.com/";
driver.get(baseUrl);
driver.manage().window().setSize(new Dimension(1920, 1080));
WebElement Users = driver.findElement(By.xpath("/html/body/header/div/div[1]/nav/ol/li[5]"));
Thread.sleep(3000);
Actions builder = new Actions(driver);
builder.moveToElement(Users).perform();
Thread.sleep(3000);
driver.switchTo().activeElement().click();
}
}
I am not able to understand why I am not getting the expected output. Please help.
I don't know why Actions class is not focusing the element. Its an issue or something as so many time i have trouble with Actions class in Firefox browser.
Still you have one alternative way to Focus on desired element and then perform click on focused element i.e. JavascriptExecutor
Here you is your code :
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('nav-users').focus();");
System.out.println(driver.switchTo().activeElement().getTagName());
driver.switchTo().activeElement().click();

Why doesn't Selenium find element by xpath?

When I run the following program, why is '0' printed to the console? I expected '1' to be printed since I expected the findElements() method to find a link using the xpath. Is the xpath expression incorrect? I got the expression using Firefox, Firebug, and Firepath, by selecting the link element and copying the given xpath.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import java.util.List;
public class SeleniumSearch {
static WebDriver driver = new FirefoxDriver();
public static void main(String[] args) {
try {
driver.get("http://www.google.co.uk/");
submitSearch("selenium");
getHit();
}
finally {
driver.close();
}
}
static void submitSearch(String search) {
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys(search);
searchBox.submit();
}
static void getHit() {
List<WebElement> hits = driver.findElements(By.xpath("html/body/div[5]/div[4]/div[9]/div[1]/div[3]/div/div[3]/div[2]/div/div/div/div[2]/div[1]/div/h3/a"));
System.out.println(hits.size());
}
}
Try putting the following as xpath instead of the actual path:
//*[#id="rso"]/div[2]/div[1]/div/h3/a
xpath("html/body/div[5]/div[4]/div[9]/div[1]/div[3]/div/div[3]/div[2]/div/div/div/div[2]/div[1]/div/h3/a")
That's wrong work with xpath, one little change on website and your code wouldn't work! try to do it more dynamic find the closest id or tag name and continue from there, can you share your html source?
I would use a simple xpath like html/body//h3/a.
You can also use FirePath extension of FireBug to build and evaluate xpaths.
Simplest xpath I could come up with for first link in google search:
(//h3/a)[1]

Selenium Webdriver pass email to form by js

How can I pass email into this form?
You need just click "Professional - Start free trial", which is where I need pass email using selenium webdriver.
I noticed that the popup for providing your email is in a different frame. You should manage Context switching:
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Email extends BrowserFunctions{
public static void main(String args[]) throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "Drivers//chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.wrike.com/price/");
Thread.sleep(5000);
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.id("start-free-trial-professional")));
Thread.sleep(5000);
driver.switchTo().defaultContent(); // you are now outside both frames
driver.switchTo().frame("ajaxIframeFeatures");
executor.executeScript("arguments[0].setAttribute('value', 'abc#gmail.com');", driver.findElement(By.id("email")));
executor.executeScript("arguments[0].click();", driver.findElement(By.id("start-project")));
Thread.sleep(5000);
}
}
Sorry for using too much of Thread.sleep(), it's not a good practice generally. You can replace it by WebDriverWait if you want to improve the performance.
I hope it helps :)

Categories