Node.js Can';使用Xpath查找元素(Selenium使用Jest)
我只是试着用selenium和jest测试一个网站。但是当我使用Node.js Can';使用Xpath查找元素(Selenium使用Jest),node.js,selenium-webdriver,xpath,jestjs,Node.js,Selenium Webdriver,Xpath,Jestjs,我只是试着用selenium和jest测试一个网站。但是当我使用driver.findElement(By.xpath())时,该方法无法找到元素。发出了这样的错误 NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="root"]/div[2]/butto
driver.findElement(By.xpath())
时,该方法无法找到元素。发出了这样的错误
NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="root"]/div[2]/button[2]"}
(Session info: chrome=84.0.4147.89)
31 |
32 | await driver.findElement(By.xpath(`//*[@id="root"]/div[1]/ul[2]/li[2]/button`)).click();
> 33 | await driver.findElement(By.xpath(`//*[@id="root"]/div[2]/button[2]`)).click();
这是我的密码
const webdriver = require('selenium-webdriver');
const {By, until, Navigation, Builder} = require('selenium-webdriver');
const chromedriver = require('chromedriver');
const chrome = require ('selenium-webdriver/chrome');
const assert = require('assert');
describe('Test Login with Jest', function() {
let driver;
jest.setTimeout(30000);
beforeAll(async () => {
chrome.setDefaultService(new chrome.ServiceBuilder(chromedriver.path).build());
driver = await new Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
});
afterAll(async () => {
await driver.quit();
});
it("SEMANGAT GUH !", async () => {
await driver.get("https://genflix.co.id");
let titleGenflix = await driver.getTitle();
await driver.findElement(By.xpath(`//*[@id="root"]/div[1]/ul[2]/li[2]/button`)).click();
await driver.findElement(By.xpath(`//*[@id="root"]/div[2]/button[2]`)).click();
await driver.findElement(By.xpath(`//*[@id="email"]`)).sendKeys("my_email");
await driver.findElement(By.xpath(`//*[@id="password"]`)).sendKeys("my_password");
await driver.findElement(By.xpath(`/html/body/div[1]/div[2]/form/button`)).click();
assert.equal(titleGenflix, "Genflix");
}, 10000);
});
我希望有人能帮助我,
谢谢你提供了一些线索。更改代码并使用相对XPath单击
登录
按钮(使用直到
条件和执行脚本
执行单击):
对脚本的其余部分执行相同的操作(即使是sendkeys操作(直到找到>直到启用>单击>sendkeys))。最好使用相对XPath:
//button[.="Login"]
//button[.="Login with Email/UserID"]
//input[@id="email"]
//input[@id="password"]
//button[@type="submit"]
旁注:您可以直接从以下url开始执行登录步骤(并删除您的2次首次单击):
请始终从脚本中删除您的登录凭据。@E.Wiest感谢您提醒我并编辑我的帖子。
//button[.="Login"]
//button[.="Login with Email/UserID"]
//input[@id="email"]
//input[@id="password"]
//button[@type="submit"]
https://genflix.co.id/login/email