无法从selenium webdriver中的下拉列表中选择项目

无法从selenium webdriver中的下拉列表中选择项目,selenium,dropdown,Selenium,Dropdown,我正在尝试从下拉列表中选择一个项目,因为在html中,标记没有用作下拉列表的选择,然后我通过操作选择下拉列表项目。 我的问题是,在这种情况下,这是选择下拉项的标准编码方式,还是我需要更改代码 import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.sele

我正在尝试从下拉列表中选择一个项目,因为在
html
中,标记没有用作下拉列表的选择,然后我通过操作选择下拉列表项目。

我的问题是,在这种情况下,这是选择下拉项的标准编码方式,还是我需要更改代码

    import org.openqa.selenium.By;
    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.interactions.Actions;
    import org.openqa.selenium.support.ui.Select;

    public class test 
    {
    static WebDriver driver;
    public static void main(String[] args) 
    {
        System.setProperty("webdriver.chrome.driver", "D:\\rakesh\\software\\selenium browser\\New folder\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.get("https://app.crossover.com/x/marketplace/available-jobs");
        driver.manage().window().maximize();
        driver.findElement(By.cssSelector(".btn.btn-default.form-control.ui-select-toggle")).click();

        WebElement dropdonw_ele = driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/div/div/div[2]/form/div/div[2]/job-label-select/div/ul/li/div[5]/span/div/span"));
        Actions act = new Actions(driver);
        act.build();
        act.moveToElement(dropdonw_ele).click();
        act.perform();
    }
}

如果您的下拉列表是本机html下拉列表,请查看org.openqa.selenium.support.ui.Select类

//simplify this xpath expression?
WebElement dropdonw_ele = driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/div/div/div[2]/form/div/div[2]/job-label-select/div/ul/li/div[5]/span/div/span"));
Select dropDown = new Select(dropdonw_ele);
dropDown.selectByValue("your item value");
如果没有原生html标记,则可以使用操作,也可以直接单击值框而不使用操作。如果由于下拉值元素不可见而无法工作,则可以使用JavaScript单击它

private void clickWithJavaScript(WebElement target) {
        JavascriptExecutor executor = (JavascriptExecutor) driver;
        executor.executeScript("arguments[0].click()", target);
}  

嗨,洛基,你可能不需要使用动作类。 下面是可行的代码,你可以尝试在你的最后,让我知道

public class test 
{
static WebDriver driver;
public static void main(String[] args) 
   {
    System.setProperty("webdriver.chrome.driver", "D:\\rakesh\\software\\selenium browser\\New folder\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();    
    driver.get("https://app.crossover.com/x/marketplace/available-jobs");
    Thread.sleep(3000L);
    driver.findElement(By.xpath(".//*[@ng-click='$select.toggle($event)']")).click();
    Thread.sleep(3000L);
    driver.findElement(By.xpath(".//*[contains(text(),'iOS and Android')]")).click();
    Thread.sleep(3000L);
   }
}
请在你这边试试。 快乐学习:-)

试试这个方法。
注意:使用
相对xpath
,而不是
绝对xpath

driver.get("https://app.crossover.com/x/marketplace/available-jobs");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("//span[@class='ui-select-match-text pull-left']/span")).click();            //click on All job categories first
Thread.sleep(3500);
driver.findElement(By.xpath("//div[@class='ng-scope']/span[contains(text(), 'C++')]")).click();     //By using xpath method click on C++ from the dropdown selection.
Thread.sleep(3500);

有什么反应吗@rockyok Thank@jainish事实上,我在相对xpath方面遇到了问题,因为页面上的某些元素会动态变化。如果您的查询得到解决,请将此答案标记为
已接受
。因为这对其他用户也有帮助。:)