英文:
selenium.ElementNotInteractableException: element not interactable: Element is not currently visible and may not be manipulated error Selenium Java
问题
I want to select the Code 128 (standard) from select menu, but whatever i did, i could not manage to select any option from the menu.
我想从选择菜单中选择Code 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
BrowserUtils.waitFor(4);
System.out.println("options.size() = " + options.size());
select.selectByVisibleText("Code 128 (standard)");
driver.findElement(By.id("barcode_data")).sendKeys("abc");
And I got this response:
然后我得到了以下回应:
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'
英文:
I want to select the Code 128 (standard) from select menu, but whatever i did, i could not manage to select any option from the menu.
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");
And I got this response:
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'
答案1
得分: 0
尽管<option>
中包含文本 Code 128 (standard),但 <select>
标签包含属性 style="display: none;"
:
<select style="color: rgb(0, 0, 0); display: none;" id="code_selector">
因此,Selenium 将无法与其交互。
解决方案
要单击包含文本 Code 128 (standard) 的元素,您需要 Mouse Hover 到父元素,然后单击文本为 Create Barcode 的 <button>
,您可以使用以下 定位策略:
-
代码块:
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();
-
浏览器快照:
英文:
Though the <option>
with text Code 128 (standard) is within the [tag:html-select] tag but the <select>
tag contains the attribute style="display: none;"
:
<select style="color: rgb(0, 0, 0); display: none;" id="code_selector">
Hence Selenium won't be able to interact with it.
Solution
To click on the element with text with text Code 128 (standard) you need to Mouse Hover the parent element and then click on the <button>
with text Create Barcode and you can use the following locator strategies:
-
Code Block:
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();
-
Browser Snapshot:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论