如何使用 CssSelectors
How to use CssSelectors
我想用 Xpath 测试 ebay 站点登录。但它不起作用我试了很多小时。请解释错误。
1. 打开Google 网站
2.搜索“ebay”网站
3.点击相应的结果
4. 前往 http://www.ebay.com 网站
5. 点击“Sign in”link导航到ebay登录页面
6. 输入邮箱 address/password
7. 点击登录按钮
8. 验证您的用户名。
包脚本;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class TestEbay_login {
@Test
public void ebay_login() throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "C:\Users\Senani\Downloads\Programs\geckodriver-v0.23.0-win64\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//driver.get("https://www.google.com");
driver.navigate().to("https://www.google.com");
driver.manage().window().maximize();
driver.findElement(By.name("q")).sendKeys("ebay");
driver.findElement(By.name("btnK")).submit();
driver.findElement(By.cssSelector("a[href='www.ebay.com']")).click();
driver.findElement(By.linkText("Sign in")).click();
driver.findElement(By.id("userid")).sendKeys("#########");
driver.findElement(By.id("pass")).sendKeys("###########");
driver.findElement(By.id("sgnBt")).click();
}
}
FAILED: ebay_login org.openqa.selenium.NoSuchElementException: Unable to locate element: a[href='www.ebay.com'] For documentation on
this error, please visit:
https://www.seleniumhq.org/exceptions/no_such_element.html Build info:
version: '3.141.59', revision: 'e82be7d358', time:
'2018-11-14T08:25:48'
这里有两个问题要运行:
首先,虽然 text Google 显示在搜索结果中说 URL 是 https://www.ebay.com
,但 <a>
标签的 actual href
是用于跟踪目的的内部(乱码)link。因此,在我的浏览器中,eBay 结果的 href
实际上指向:
/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=2ahUKEwjItIGq48_fAhWiSt8KHVHWCMUQFjAAegQIARAC&url=https%3A%2F%2Fwww.ebay.com%2F&usg=AOvVaw2IUWs7JZelxpS-zydrZoSX
...当然会 重定向 到 eBay 的网站。
您将遇到的第二个问题是 CSS 选择器 a[href='www.ebay.com']
需要 精确 匹配——该选择器将 not 匹配指向 https://www.ebay.com
.
的 link
编辑:深入研究 Google 结果页面上的 HTML,看起来 边栏 中的 link 实际上确实存在直接到 eBay.com。因此,您可以将 CSS 选择器编辑为:a[href='http://www.ebay.com']
(请注意,由于我不清楚的原因,侧边栏 link 转到 eBay 主页的非安全版本。)
启动搜索后,您需要诱导 WebDriverWait
所需的 元素可点击,您可以使用以下解决方案:
new WebDriverWait(driver, 20).until(
ExpectedConditions.elementToBeClickable(By.cssSelector("a[href='https://www.ebay.com/']"))
).click();
我想用 Xpath 测试 ebay 站点登录。但它不起作用我试了很多小时。请解释错误。
1. 打开Google 网站
2.搜索“ebay”网站
3.点击相应的结果
4. 前往 http://www.ebay.com 网站
5. 点击“Sign in”link导航到ebay登录页面
6. 输入邮箱 address/password
7. 点击登录按钮
8. 验证您的用户名。
包脚本;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class TestEbay_login {
@Test
public void ebay_login() throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "C:\Users\Senani\Downloads\Programs\geckodriver-v0.23.0-win64\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//driver.get("https://www.google.com");
driver.navigate().to("https://www.google.com");
driver.manage().window().maximize();
driver.findElement(By.name("q")).sendKeys("ebay");
driver.findElement(By.name("btnK")).submit();
driver.findElement(By.cssSelector("a[href='www.ebay.com']")).click();
driver.findElement(By.linkText("Sign in")).click();
driver.findElement(By.id("userid")).sendKeys("#########");
driver.findElement(By.id("pass")).sendKeys("###########");
driver.findElement(By.id("sgnBt")).click();
}
}
FAILED: ebay_login org.openqa.selenium.NoSuchElementException: Unable to locate element: a[href='www.ebay.com'] For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
这里有两个问题要运行:
首先,虽然 text Google 显示在搜索结果中说 URL 是 https://www.ebay.com
,但 <a>
标签的 actual href
是用于跟踪目的的内部(乱码)link。因此,在我的浏览器中,eBay 结果的 href
实际上指向:
/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=2ahUKEwjItIGq48_fAhWiSt8KHVHWCMUQFjAAegQIARAC&url=https%3A%2F%2Fwww.ebay.com%2F&usg=AOvVaw2IUWs7JZelxpS-zydrZoSX
...当然会 重定向 到 eBay 的网站。
您将遇到的第二个问题是 CSS 选择器 a[href='www.ebay.com']
需要 精确 匹配——该选择器将 not 匹配指向 https://www.ebay.com
.
编辑:深入研究 Google 结果页面上的 HTML,看起来 边栏 中的 link 实际上确实存在直接到 eBay.com。因此,您可以将 CSS 选择器编辑为:a[href='http://www.ebay.com']
(请注意,由于我不清楚的原因,侧边栏 link 转到 eBay 主页的非安全版本。)
启动搜索后,您需要诱导 WebDriverWait
所需的 元素可点击,您可以使用以下解决方案:
new WebDriverWait(driver, 20).until(
ExpectedConditions.elementToBeClickable(By.cssSelector("a[href='https://www.ebay.com/']"))
).click();