Selenium WebDriver Java - 无法点击自动完成

Selenium WebDriver Java - Can't click on Auto Completion

将密钥发送到字段后,我无法单击自动完成选项。 我能够列出它们,确定我想要 select 的那个,但显然有问题。这是规格,

我希望能够 select 'Sign Up' 上此网页“https://supercareer.com/home/company”的位置字段,我 select,发送密钥,点击建议的位置,但它没有 select 选项。我在 Java 控制台上没有收到任何错误。

我用来 select 自动完成功能的功能:

public static void autoCompleteOptionSelector(String autoCompleteSearchText, String selection){

        WebElement autoOptions= driver.findElement(By.xpath("/html/body/div[3]/div[2]/div[3]/form/div[2]/div[2]/div[3]/span[1]/input"));
        autoOptions.sendKeys(autoCompleteSearchText);


        List<WebElement> optionsToSelect = driver.findElements(By.xpath("//div[@class='tt-suggestion tt-selectable']"));
        System.out.println("Size of the AutoSuggets is = " + optionsToSelect.size());

    //I print options till match with exact option I want. and then Click
        for(WebElement option : optionsToSelect){
            System.out.println(option);
            System.out.println("Values are = " + option.getText());
            if(option.getText().equals(selection)) {
                System.out.println("Trying to select: "+ selection);
                option.click();
                break;
            }
        }
    }

这是我的Main,CaseFunctions是上面函数的class:

WebDriver dr = new FirefoxDriver();
    CaseFunctions.autoCompleteOptionSelector("18661","White Haven, PA 18661, United States");

这是我的输出,没有错误,但在网页上我无法 select。

控制台输出:

AutoSuggets 的大小为 = 5

[[FirefoxDriver: WINDOWS (b676fd34-8272-47c7-b0bc-14aace352dd6)] -> xpath: //div[@class='tt-suggestion tt-selectable']]

值为美国宾夕法尼亚州怀特黑文 18661

正在尝试select:美国宾夕法尼亚州怀特黑文 18661

我觉得一切都很好。

我想,除了 if 中的条件外,其他都正确。我们需要使用 contains 而不是 equals。下面的代码可能会给你一些想法。

    if(option.getText().contains(selection)) 
    {
        System.out.println("Trying to select: "+ selection);
        option.click();
        break;
    }

完整代码:

public static void main(String[] args) 
{
    System.setProperty("webdriver.chrome.driver", "/home/santhoshkumar/Softwares/Selenium/drivers/chromedriver2.29");
    WebDriver driver = new ChromeDriver();
    driver.get("https://supercareer.com/home/company");
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    driver.findElement(By.xpath("//*[@id=\"AnonymousHeader1\"]/div/div[2]/div/div/a[1]")).click();
    WebElement autoOptions= driver.findElement(By.xpath("/html/body/div[3]/div[2]/div[3]/form/div[2]/div[2]/div[3]/span[1]/input"));
    autoOptions.sendKeys("18661");


    List<WebElement> optionsToSelect = driver.findElements(By.xpath("//div[@class='tt-suggestion tt-selectable']"));
    System.out.println("Size of the AutoSuggets is = " + optionsToSelect.size());

    //I print options till match with exact option I want. and then Click
    for(WebElement option : optionsToSelect)
    {
        System.out.println(option);
        System.out.println("Values are = " + option.getText());
        if(option.getText().contains("White Haven, PA 18661, United States")) 
        {
            System.out.println("Trying to select: "+ "White Haven, PA 18661, United States");
            option.click();
            break;
        }
    }

}

希望对您有所帮助。谢谢

您可以尝试使用 JavaScript 执行器单击该选项吗?请将代码 option.click() 替换为以下内容。

JavaScriptExecutor js = (JavaScriptExecutor) driver;
js.executeScript ("arguments [0].click ()", option);

在尝试了很多事情并尝试了 "Santhosh Kumar" 的一些友好建议之后(无论如何感谢)。

这个东西解决了关于firefox的问题,但我仍然想知道为什么?因为对于任何其他情况,在这个位置自动完成字段之前,Firefox 驱动程序中从未遇到过这样的问题。

DesiredCapabilities capability = DesiredCapabilities.firefox();