简体   繁体   中英

Xpath in Selenium

I want to write xpath for the below flow :

  1. Go to au.support.tomtom.com
  2. click on Flag sign in the footer.
  3. select Australia as a country

So far I have tried:

driver.get("http://au.support.tomtom.com/");
driver.findElement(By.xpath(".//*[@id='rn_PageFooter_16']/footer/div[3]/button")).click();
driver.findElement(By.xpath("//a[@href='//au.support.tomtom.com/app/answers/list/locale/en_AU']//[@text()='Australia']")).click();

Last xpath is not working. I am using selenium JavaScript. Please help.

I think it was your xpath which was causing the problem also your element was not on the view to be clickable

This below code is tested and worked for me

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class test1 {

@Test
public void chec() throws InterruptedException{

    System.setProperty("webdriver.chrome.driver","C://Temp//imp//chromedriver.exe");
    WebDriver d = new ChromeDriver();
    d.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
    d.get("http://au.support.tomtom.com/");
    d.manage().window().maximize();
    JavascriptExecutor jse = (JavascriptExecutor)d; 
    d.findElement(By.xpath(".//*[@id='rn_PageFooter_16']/footer/div[3]/button")).click();
    Thread.sleep(5000);
    jse.executeScript("scroll(0, -250);");
    d.findElement(By.xpath("html/body/div[4]/div/div[2]/footer/div[2]/div/div/div/div[1]/div/div[1]/div/div[2]/ul/li[1]/a")).click();


}
}

Why use XPath? It's much easier to find the Australia flag using CSS selector:

li .tt-flag-image.australia

Full code:

driver.findElement(By.cssSelector("li .tt-flag-image.australia")).click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM