selenium.ElementNotInteractableException:元素不可交互:元素当前不可见,可能无法操作错误Selenium Java

5t7ly7z5  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(96)

我想从选择菜单中选择代码128(标准),但无论我做了什么,我都无法从菜单中选择任何选项。

WebDriver   driver = Driver.get();
driver.manage().window().setPosition(new Point(-1000, 0));
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.barcode-generator.org/");
BrowserUtils.waitFor(2);
driver.findElement(By.className("iubenda-cs-close-btn")).click();
BrowserUtils.waitFor(2);
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement selectElement = driver.findElement(By.xpath("//select[@id='code_selector']"));
Select select = new Select(selectElement);
List<WebElement> options = select.getOptions();
BrowserUtils.waitFor(4);
System.out.println("options.size() = " + options.size());
select.selectByVisibleText("Code 128 (standard)");
driver.findElement(By.id("barcode_data")).sendKeys("abc");

我得到了这样的回应:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052}) on port 53482
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Haz 18, 2023 1:53:26 ÖS org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
options.size() = 67

org.openqa.selenium.ElementNotInteractableException: element not interactable: Element is not currently visible and may not be manipulated
  (Session info: chrome=114.0.5735.134)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
egmofgnx

egmofgnx1#

虽然带有文本***Code 128(standard)***的<option>html-select标记内,但<select>标记包含属性style="display: none;"

<select style="color: rgb(0, 0, 0); display: none;" id="code_selector">

因此,Selenium将无法与它互动。

解决方案

要点击元素的文本***Code 128(standard)***,需要Mouse Hover父元素,然后点击<button>,文本*Create Barcode,您可以使用以下locator strategies

  • 代码块:
driver.get("https://www.barcode-generator.org/");
new WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.iubenda-cs-close-btn"))).click();
new Actions(driver).moveToElement(new WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("figure#code_figure_20")))).build().perform();
new WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='create-overlay']//div[@class='inner-cell']//button[@data-choose='20' and contains(., 'Create Barcode')]"))).click();
  • 浏览器快照:

相关问题