Xpath in Selenium
Xpath in Selenium
我想为以下流编写xpath
:
- 转到au.support.tomtom.com
- 单击页脚中的Flag标志
- 选择澳大利亚作为国家
到目前为止,我已经尝试过:
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();
最后一个xpath
不工作。我正在使用selenium JavaScript。请帮忙。
我认为是您的xpath造成了问题,而且您的元素在视图中不可点击
以下代码经过测试并适用于我
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();
}
}
为什么要使用XPath?使用CSS选择器可以更容易地找到澳大利亚国旗:
li .tt-flag-image.australia
完整代码:
driver.findElement(By.cssSelector("li .tt-flag-image.australia")).click();
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- Node.js's Buffer.writeFloatBE in Javascript
- WebDriverException:tinyMCE未在selenium Web driver java中定义
- python-selenium-点击上升链接
- Selenium Web驱动程序和javascript
- Setting default onclick behavior for <img> tag in gene
- Selenium WebDriver and JavaScript change
- Lucene Search in Alfresco
- grep in JQuery to C#
- write HTML in JavaScript
- jQuery setTimeOut: in for-loop
- SetTimeout and clearTimeout in Javascript
- lightbox in html 5 and javascript
- HTMLInputElement in IE7
- Mongodb$in以与数组中相同的顺序获取结果
- element.dataset in Internet Explorer
- Selenium驱动程序不反映单击()后的页面更改
- Xpath in Selenium
- javascript in selenium RC
- Selenium Exception : "return not in a function"