簡體   English   中英

使用 Selenium WebDriver 和 JavaScript 從 XPath 元素獲取文本

[英]Get text from XPath Element using Selenium WebDriver with JavaScript

我正在嘗試使用 Selenium 從使用 Node.js 的網站獲取文本元素。

例如,我使用的 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

我可以在 XPaath 選擇器插件中看到它導致“添加到籃子”,但是下面的代碼記錄了一個空白字符串。 知道我做錯了什么嗎?

我已經在頁面上使用不同的元素嘗試過它,所以我假設它是一個按鈕?

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)

使用以下xpath來標識按鈕。

//div[@class='purchase-info']//input[@value='Add to Basket']

更新:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM