[英]Get text from XPath Element using Selenium WebDriver with JavaScript
I'm trying to use Selenium to get text elements from websites using Node.我正在尝试使用 Selenium 从使用 Node.js 的网站获取文本元素。
As an example, I am using the URL: https://www.ebuyer.com/874785-exdisplay-gigabyte-geforce-rtx-2080-ti-gaming-oc-11gb-graphics-card-ebr2-gv-n208tgaming-oc-11gc and the XPath /html/body/section/div[1]/div[3]/div[1]/div[3]/form/button
例如,我使用的 URL: https : //www.ebuyer.com/874785-exdisplay-gigabyte-geforce-rtx-2080-ti-gaming-oc-11gb-graphics-card-ebr2-gv-n208tgaming- oc-11gc和 XPath
/html/body/section/div[1]/div[3]/div[1]/div[3]/form/button
I can see in an XPaath selector plugin that it results in "Add to Basket", however the below code logs a blank string.我可以在 XPaath 选择器插件中看到它导致“添加到篮子”,但是下面的代码记录了一个空白字符串。 Any idea what I'm doing wrong?
知道我做错了什么吗?
I've tried it with a different element on the page which works, so I'm assuming because its a button?我已经在页面上使用不同的元素尝试过它,所以我假设它是一个按钮?
const selenium = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const chromedriver = require('chromedriver');
chrome.setDefaultService(new chrome.ServiceBuilder(chromedriver.path).build());
const driver = new selenium.Builder()
.withCapabilities(selenium.Capabilities.chrome())
.build();
await driver.get(url);
await delay(1000);
const value = await driver.findElement(web.By.xpath(xPath)).getText();
console.log(value)
Use the following xpath
to identify the button.使用以下
xpath
来标识按钮。
//div[@class='purchase-info']//input[@value='Add to Basket']
Update:更新:
const value = await driver.findElement(web.By.xpath("//div[@class='purchase-info']//input[@value='Add to Basket']")).getAttribute("value");
console.log(value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.