Selenium 无法选择下拉列表,显示不可见
对于以下代码,无法选择“摊销”下拉列表Selenium 无法选择下拉列表,显示不可见,selenium,webdriver,Selenium,Webdriver,对于以下代码,无法选择“摊销”下拉列表 import java.awt.Toolkit; import org.junit.Assert; import org.openqa.selenium.By; import org.openqa.selenium.Dimension; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Keys;
import java.awt.Toolkit;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class PracticeTest {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
WebDriverWait wait;
System.setProperty("webdriver.chrome.driver", "C:\\Users\\dilu316\\Downloads\\selenium workspace\\chromedriver\\chromedriver.exe");
//approach 1 - to maximize screen in chrome
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");;
driver = new ChromeDriver(options);
wait=new WebDriverWait(driver, 50);
//if the below method wont work for maximizing screen then above method we can use
//WebDriver driver = new ChromeDriver();
//Thread.sleep(5000);
//driver.manage().window().maximize();
driver.get("http://ia.ca/");
//approach 2 - to maximize screen in chrome
/*Toolkit toolkit = Toolkit.getDefaultToolkit();
int height = (int)toolkit.getScreenSize().getHeight();
int width = (int)toolkit.getScreenSize().getWidth();
System.out.println(height + "--" + width);
driver.manage().window().setSize(new Dimension(width, height));*/
driver.findElement(By.xpath("//*[@id='nav-secondaire']//a[@data-utag-name='loans']")).click();
driver.findElement(By.xpath("//a[contains(text(),'Mortgages')]")).click();
//in the upper xpath we can put a ". in place of text()" also
//method 1 -if element is not clickable
/*WebDriverWait wait = new WebDriverWait(driver, 100);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Calculate your payments')]")));*/
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("scroll(255, 644)");
driver.findElement(By.xpath("//a[contains(.,'Calculate your payments')]")).click();
WebElement priceSlideLocator = driver.findElement(By.xpath("//div[@class='slider-handle min-slider-handle custom']"));
WebElement slideTrack = driver.findElement(By.xpath("//div[@class='slider-track-high']"));
Dimension sliderSize = slideTrack.getSize();
int sliderWidth = sliderSize.getWidth();
int xCoord = priceSlideLocator.getLocation().getX();
System.out.println(xCoord);
System.out.println(sliderWidth);
Thread.sleep(10);
Actions builder = new Actions(driver);
builder.moveToElement(priceSlideLocator)
.click()
.dragAndDropBy(priceSlideLocator, xCoord+sliderWidth,0)
.build()
.perform();
WebElement hiddenPriceLocator = driver.findElement(By.xpath("//input[@id='sliderPrixPropriete']"));
int priceValue = Integer.parseInt(hiddenPriceLocator.getAttribute("value"));
if(priceValue==2000000){
System.out.println("price value is 2000000");
}
String stylePercent = priceSlideLocator.getAttribute("style");
if(stylePercent.contains("left: 100%")){
System.out.println("slide is 100%");
}
priceSlideLocator = driver.findElement(By.xpath("//div[@class='slider-handle min-slider-handle custom']"));
slideTrack = driver.findElement(By.xpath("//div[@class='slider-track-high']"));
sliderSize = slideTrack.getSize();
//sliderWidth = sliderSize.getWidth();
xCoord = priceSlideLocator.getLocation().getX();
System.out.println(xCoord);
Thread.sleep(10);
Actions builder2 = new Actions(driver);
builder2.moveToElement(priceSlideLocator).click().dragAndDropBy(priceSlideLocator,-(xCoord+sliderWidth),0).build().perform();
System.out.println("slide BACK%");
hiddenPriceLocator = driver.findElement(By.xpath("//input[@id='sliderPrixPropriete']"));
//priceValue = Integer.parseInt(hiddenPriceLocator.getAttribute("value"));
WebElement plusButton = driver.findElement(By.id("PrixProprietePlus"));
for(int i=0; i<2;i++){
plusButton.click();
priceValue = Integer.parseInt(hiddenPriceLocator.getAttribute("value"));
if(priceValue==500000){
System.out.println("purchase price is 500000");
}
}
WebElement downPlusButton = driver.findElement(By.id("MiseDeFondPlus"));
downPlusButton.click();
WebElement downHiddenPrice = driver.findElement(By.id("sliderMiseDeFond"));
String downPrice=downHiddenPrice.getAttribute("value");
int downPricevalue = Integer.parseInt(downPrice);
System.out.println("Down payment is " + downPricevalue);
code to check the visibility
if (driver.findElement(By.xpath("//select[@id='Amortissement']")).isDisplayed()) {
System.out.println("Element is Visible");
} else {
System.out.println("Element is InVisible");
}
//To check Element Present
if (driver.findElements(By.xpath("//select[@id='Amortissement']")).size() != 0) {
System.out.println("Element is Present");
} else {
System.out.println("Element is Absent");
}
//To check Enable
if (driver.findElement(By.xpath("//select[@id='Amortissement']")).isEnabled()) {
System.out.println("Element is Enable");
} else {
System.out.println("Element is Disabled");
}
/*WebElement drpAmortization = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='Amortissement']")));
Select dropDownAmortization = new Select(drpAmortization);
dropDownAmortization.selectByVisibleText("20 years");*/
WebElement drpAmortization = driver.findElement(By.xpath("//select[@id='Amortissement']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
//executor.executeScript("arguments[0].click();", drpAmortization);
executor.executeScript("window.document.getElementById('Amortissement').click()");
Select dropDownAmortization = new Select(drpAmortization);
dropDownAmortization.selectByVisibleText("20 years");
}
}
导入java.awt.Toolkit;
导入org.junit.Assert;
导入org.openqa.selenium.By;
导入org.openqa.selenium.Dimension;
导入org.openqa.selenium.JavascriptExecutor;
导入org.openqa.selenium.Keys;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.openqa.selenium.chrome.ChromeOptions;
导入org.openqa.selenium.interactions.Actions;
导入org.openqa.selenium.remote.DesiredCapabilities;
导入org.openqa.selenium.support.ui.ExpectedConditions;
导入org.openqa.selenium.support.ui.Select;
导入org.openqa.selenium.support.ui.WebDriverWait;
公开课实习测验{
公共静态void main(字符串[]args)引发InterruptedException{
网络驱动程序;
网络驱动程序让它等待;
System.setProperty(“webdriver.chrome.driver”,“C:\\Users\\dilu316\\Downloads\\selenium workspace\\chromedriver\\chromedriver.exe”);
//方法1-最大化chrome屏幕
ChromeOptions选项=新的ChromeOptions();
options.addArguments(“--start maximized”);;
驱动程序=新的色度驱动程序(可选);
等待=新的WebDriverWait(驱动程序,50);
//如果下面的方法不能使屏幕最大化,那么我们可以使用上面的方法
//WebDriver驱动程序=新的ChromeDriver();
//睡眠(5000);
//driver.manage().window().maximize();
驱动程序。获取(“http://ia.ca/");
//方法2-最大化chrome屏幕
/*Toolkit=Toolkit.getDefaultToolkit();
int height=(int)toolkit.getScreenSize().getHeight();
int width=(int)toolkit.getScreenSize().getWidth();
System.out.println(高度+“--”+宽度);
driver.manage().window().setSize(新尺寸(宽度、高度))*/
driver.findElement(By.xpath(“//*[@id='nav-secondaire']///a[@data utag name='loans']))。单击();
findElement(By.xpath(“//a[contains(text(),'Mortgages')]”)。单击();
//在上面的xpath中,我们还可以用“.”代替text()
//方法1-如果元素不可单击
/*WebDriverWait wait=新的WebDriverWait(驱动程序,100);
wait.until(ExpectedConditions.elementtobelickable(By.xpath(//a[contains(,'Calculate your payments')])*/
JavascriptExecutor js=(JavascriptExecutor)驱动程序;
js.executeScript(“滚动(255644)”;
findElement(By.xpath(//a[contains(,'Calculate your payments')])))。单击();
WebElement pricelidelocator=driver.findelelement(By.xpath(“//div[@class='slider-handle-min-slider-handle-custom']);
WebElement slideTrack=driver.findElement(By.xpath(“//div[@class='slider-track-high']);
维度sliderSize=slideTrack.getSize();
int sliderWidth=sliderSize.getWidth();
int xCoord=pricelidelocator.getLocation().getX();
System.out.println(xCoord);
System.out.println(滑块宽度);
睡眠(10);
动作生成器=新动作(驱动程序);
builder.moveToElement(PricelideLocator)
。单击()
.dragAndDropBy(priceSlideLocator,xCoord+滑块宽度,0)
.build()
.perform();
WebElement hiddenPriceLocator=driver.findElement(By.xpath(//input[@id='sliderPrixPropriete']);
int priceValue=Integer.parseInt(hiddenPriceLocator.getAttribute(“value”);
如果(价格值==2000000){
System.out.println(“价格值为2000000”);
}
String stylePercent=pricelidelocator.getAttribute(“样式”);
if(stylePercent.contains(“左:100%”){
System.out.println(“幻灯片为100%”;
}
pricelidelocator=driver.findelelement(By.xpath(“//div[@class='slider-handle-min slider-handle-custom']);
slideTrack=driver.findElement(By.xpath(“//div[@class='slider-track-high']);
sliderSize=slideTrack.getSize();
//sliderWidth=sliderSize.getWidth();
xCoord=pricelidelocator.getLocation().getX();
System.out.println(xCoord);
睡眠(10);
Actions builder2=新操作(驱动程序);
builder2.moveToElement(priceSlideLocator)。单击().dragAndDropBy(priceSlideLocator,-(xCoord+sliderWidth),0)。构建().perform();
System.out.println(“向后滑动%”;
hiddenPriceLocator=driver.findElement(By.xpath(//input[@id='sliderPrixPropriete']);
//priceValue=Integer.parseInt(hiddenPriceLocator.getAttribute(“值”);
WebElement plusButton=driver.findElement(By.id(“PrixProprietePlus”);
对于(int i=0;i我已经检查了页面的html代码,下拉列表不是传统的选择下拉列表。它只是一个按钮式的东西。因此方法如下
单击向下箭头按钮:
driver.findElement(By.xpath("//*[@id='form_calculateur_versements']/div[4]//b")).click();
现在,选项将可见
从下拉列表中选择选项:
driver.findElement(By.xpath("//*[@id='form_calculateur_versements']/div[4]//li[2]")).click();
我从上面代码的下拉列表中选择第二个选项
类似的方法适用于页面中的所有下拉列表
希望这对您有所帮助。谢谢。摊销下拉列表不是标准的选择控件,因此您使用的xpath将不起作用。此外,标准Webdriver API(如selectByVisibleText等)不能用于从此类下拉列表中选择值。但是,您可以编写自己的自定义通用方法来选择值对其进行操作,如下所示:
public static void selectText(String dropDownLabel, String value) {
String showOptions = "//label[normalize-space(text())='" + dropDownLabel + "']/following::b[1]";
driver.findElement(By.xpath(showOptions)).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
String option = "//label[normalize-space(text())='" + dropDownLabel + "']/following::li[normalize-space(text())='"
+ value + "']";
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath(option))));
driver.findElement(By.xpath(option)).click();
}
在上面的代码中,单击下拉列表后,Webdriver将等待下拉选项出现,然后单击所需选项。如果您有进一步的疑问,请告诉我。以下是您问题的答案:
关于解决方案的几句话:
在没有预期条件的情况下诱导ExplicitWait
,违背了在wa中保护自己的目的
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
public class Q44226865_select_dropdown_list
{
public static void main(String[] args) throws Exception
{
WebDriver driver;
String innerhtml = null;
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
//approach 1 - to maximize screen in chrome
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
driver.get("http://ia.ca/");
driver.findElement(By.xpath("//*[@id='nav-secondaire']//a[@data-utag-name='loans']")).click();
driver.findElement(By.xpath("//a[contains(text(),'Mortgages')]")).click();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("scroll(255, 644)");
driver.findElement(By.xpath("//a[contains(.,'Calculate your payments')]")).click();
WebElement priceSlideLocator = driver.findElement(By.xpath("//div[@class='slider-handle min-slider-handle custom']"));
WebElement slideTrack = driver.findElement(By.xpath("//div[@class='slider-track-high']"));
Dimension sliderSize = slideTrack.getSize();
int sliderWidth = sliderSize.getWidth();
int xCoord = priceSlideLocator.getLocation().getX();
System.out.println(xCoord);
System.out.println(sliderWidth);
Thread.sleep(10);
Actions builder = new Actions(driver);
builder.moveToElement(priceSlideLocator)
.click()
.dragAndDropBy(priceSlideLocator, xCoord+sliderWidth,0)
.build()
.perform();
WebElement hiddenPriceLocator = driver.findElement(By.xpath("//input[@id='sliderPrixPropriete']"));
int priceValue = Integer.parseInt(hiddenPriceLocator.getAttribute("value"));
if(priceValue==2000000){
System.out.println("price value is 2000000");
}
String stylePercent = priceSlideLocator.getAttribute("style");
if(stylePercent.contains("left: 100%")){
System.out.println("slide is 100%");
}
priceSlideLocator = driver.findElement(By.xpath("//div[@class='slider-handle min-slider-handle custom']"));
slideTrack = driver.findElement(By.xpath("//div[@class='slider-track-high']"));
sliderSize = slideTrack.getSize();
//sliderWidth = sliderSize.getWidth();
xCoord = priceSlideLocator.getLocation().getX();
System.out.println(xCoord);
Thread.sleep(10);
Actions builder2 = new Actions(driver);
builder2.moveToElement(priceSlideLocator).click().dragAndDropBy(priceSlideLocator,-(xCoord+sliderWidth),0).build().perform();
System.out.println("slide BACK%");
hiddenPriceLocator = driver.findElement(By.xpath("//input[@id='sliderPrixPropriete']"));
//priceValue = Integer.parseInt(hiddenPriceLocator.getAttribute("value"));
WebElement plusButton = driver.findElement(By.id("PrixProprietePlus"));
for(int i=0; i<2;i++){
plusButton.click();
priceValue = Integer.parseInt(hiddenPriceLocator.getAttribute("value"));
if(priceValue==500000){
System.out.println("purchase price is 500000");
}
}
WebElement downPlusButton = driver.findElement(By.id("MiseDeFondPlus"));
downPlusButton.click();
WebElement downHiddenPrice = driver.findElement(By.id("sliderMiseDeFond"));
String downPrice=downHiddenPrice.getAttribute("value");
int downPricevalue = Integer.parseInt(downPrice);
System.out.println("Down payment is " + downPricevalue);
if (driver.findElement(By.xpath("//select[@id='Amortissement']")).isDisplayed()) {
System.out.println("Element is Visible");
} else {
System.out.println("Element is InVisible");
}
//To check Element Present
if (driver.findElements(By.xpath("//select[@id='Amortissement']")).size() != 0) {
System.out.println("Element is Present");
} else {
System.out.println("Element is Absent");
}
//To check Enable
if (driver.findElement(By.xpath("//select[@id='Amortissement']")).isEnabled()) {
System.out.println("Element is Enable");
} else {
System.out.println("Element is Disabled");
}
WebElement drpAmortization = driver.findElement(By.xpath("//form[@id='form_calculateur_versements']/div[4]/div/div/div[2]/b"));
drpAmortization.click();
List <WebElement> drpAmortization_list = driver.findElements(By.xpath("//form[@id='form_calculateur_versements']//div[@class='selectric-scroll']/ul/li"));
System.out.println("Number of Elements : "+drpAmortization_list.size());
for (int i=0; i<drpAmortization_list.size(); i++)
{
WebElement my_element = drpAmortization_list.get(i);
innerhtml = my_element.getAttribute("innerHTML");
if(innerhtml.contains("20 years"))
{
Thread.sleep(3000);
my_element.click();
break;
}
}
System.out.println("Value selected from Dropdown is : "+innerhtml);
}
}