希望您一切順利,我正在嘗試使用 Selenium 在 chrome 中更改我的個人資料名稱,但似乎每次遇到此例外時我都無法訪問輸入元素: no such element: Unable to locate element: {"method": "CSS selector", "selector":"#input"}。我嘗試了從 XPath、javascript 路徑和 CSS 選擇器到部分鏈接文本的每個選擇器。它似乎有點作業的是發送driver.findElement(By.xpath("//body")).sendKeys(Keys.TAB);,但這取決于實際位置。有誰知道這個問題的解決方案或替代方案?這是我的 Java 代碼:
import java.lang.reflect.Field;
import java.math.BigInteger;
import java.security.MessageDigest;
import org.apache.commons.lang3.CharSet;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor;
import com.gargoylesoftware.htmlunit.javascript.configuration.JavaScriptConfiguration;
import java.util.*;
import java.util.Map.Entry;
public class TestTest {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver","chromedriver.exe");
WebDriver driver = new ChromeDriver();
ChromeDriver driver = new ChromeDriver(options);
driver.get("chrome://settings/manageProfile");
JavascriptExecutor js = (JavascriptExecutor) driver;
// driver.findElement(By.xpath("//body")).sendKeys(Keys.TAB);
// driver.findElement(By.xpath("//body")).sendKeys(Keys.TAB);
driver.findElement(By.cssSelector("#inner-input-container")).sendKeys("Ayoub");
// driver.close();
}
}
謝謝
uj5u.com熱心網友回復:
問題是這里的 Shadow-root 元素。在 selenium 中處理陰影根元素,你需要做一些額外的作業。首先展開 shadow-root 元素,然后訪問該元素。
public WebElement expandRootElement(WebElement element) {
WebElement ele = (WebElement) ((JavascriptExecutor)driver)
.executeScript("return arguments[0].shadowRoot", element);
return ele;
}
https://www.seleniumeasy.com/selenium-tutorials/accessing-shadow-dom-elements-with-webdriver
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/512215.html
