繁体   English   中英

**如何从数量输入值中选择指定数量的IMG标签**

[英]**How to select specified number of IMG tags from quantity input value**

图片示例:

在此处输入图片说明

从单击上面的图像示例链接可以看出,“EE”是行,“8”是项目编号。 我想选择位于同一行的以下四个 IMG 标记项中的三个并回显结果。

<div id="surface" style="width: 4567px; height: 4137px; left: -1850px; top: -1152px; cursor: default;">


<img src="https://media.memories.png" data-items="L:106|EE:5" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2221px; top: 1561px; display: block;">


<img src="https://media.memories.png" data-items="L:106|EE:6" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2237px; top: 1561px; display: block;">


<img src="https://media.memories.png" data-items="L:106|EE:7" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2253px; top: 1561px; display: block;">


<img src="https://media.memories.png" data-items="L:106|EE:8" data-pl="1" style="position: absolute; cursor: pointer; width: 14px; height: 14px; left: 2269px; top: 1561px; display: block;">


</div>

我想出了如何使用以下 xpath 通过指定的行和项目编号选择上述 img 标签之一,但是如何在从下拉菜单中选择数字 3 后选择“EE”行中的三个项目?

Xpath=//img[@data-items = ' L:106|EE:8'] 

示例下拉菜单:

 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> .dropbtn { background-color: #4CAF50; color: white; padding: 16px; font-size: 16px; border: none; } .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: #f1f1f1; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 1; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } .dropdown-content a:hover {background-color: #ddd;} .dropdown:hover .dropdown-content {display: block;} .dropdown:hover .dropbtn {background-color: #3e8e41;} </style> </head> <body> <h2>Select Quantity</h2> <p>Move the mouse over the button to open the dropdown menu.</p> <div class="dropdown"> <button class="dropbtn">Item Count</button> <div class="dropdown-content"> <a href="#"> 1</a> <a href="#"> 2</a> <a href="#"> 3</a> </div> </div> </body> </html>

如果您想从下拉列表中选择3 ,您可以使用下面的 xpath。

//div[@class='dropdown-content']/a[normalize-space(.)='3']

您可以单击//button[.='Item Count']然后使用 xpath 选择3

或者,您可以使用 js 选择3而无需单击按钮。

WebElement element = driver.findElement(By.xpath("//div[@class='dropdown-content']/a[normalize-space(.)='3']"));
JavascriptExecutor js = (JavascriptExecutor) driver;  
js.executeScript("arguments[0].click();",element);

您可以list所有图像,然后使用sublist仅迭代 3 个图像。 请看看下面的代码。

WebDriverWait wait = new WebDriverWait(driver, 40);
List<WebElement> allImages = driver.findElements(By.cssSelector("div#surface > img"));
for (WebElement image : allImages.subList(1, allImages.size())) 
    wait.until(ExpectedConditions.elementToBeClickable(image));
    image.click();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM