简体   繁体   English

带查找元素命令的字符串变量Java

[英]String Variable with Find Element Commands Java

Hello all the following codes works here it is. 您好以下所有代码都可以在这里工作。

String content = FileUtils.readFileToString(new File("C:\\PayrollSync\\prepayroll.txt"));
        String [] Arrayclients = content.split("\n");
        // begin for loop
        for(String client : Arrayclients) { 

        PP_OBJ_CycleData.verifysearch(driver);
        PP_OBJ_CycleData.searchbox(driver).clear();         
        PP_OBJ_CycleData.searchbox(driver).sendKeys(client);
        PP_OBJ_CycleData.searchbox(driver).sendKeys(Keys.BACK_SPACE);
        Thread.sleep(4000);

        //WebElement dropdown = driver.findElement(By.xpath(".//*[@title="+client+"]"));
        //dropdown.click();
        //driver.findElement(By.xpath(".//*[(text(),"+client+"]")).click();
        driver.findElement(By.xpath("//*[contains(text(),"+client+")]")).click();;
         Thread.sleep(2000);

        PP_OBJ_CycleData.practitioner(driver).click();

The Problem: None for the driver.findElements are working such as: 问题:driver.findElements无效,例如:

//WebElement dropdown = driver.findElement(By.xpath(".//*[@title="+client+"]"));
        //dropdown.click();
        //driver.findElement(By.xpath(".//*[(text(),"+client+"]")).click();
        driver.findElement(By.xpath("//*[contains(text(),"+client+")]")).click();;
         Thread.sleep(2000);

My failure traces says cannot find element By.xpath("//*[contains(text(),"+client+")]")). 我的失败痕迹表明找不到元素By.xpath(“ // * [contains(text(),” + client +“)]”))))。 However I am waiting for the element to be visible when i go to the next page. 但是,当我转到下一页时,我正在等待该元素可见。 My wait is below. 我的等待在下面。

 Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(20, TimeUnit.SECONDS);
           element = driver.findElement(By.id("toolbarQuickSearch"));

           wait.until(ExpectedConditions.or(
                ExpectedConditions.visibilityOf(element),
                ExpectedConditions.elementToBeClickable(element)
            ));

The problem from what I can see if that after the variable that contains the string is entered into the search field and the string name is visible selenium cant find the string name to click on for example below: 从我能看到的问题来看,是否在将包含字符串的变量输入到搜索字段中并且字符串名称可见之后,硒无法找到要单击的字符串名称,例如以下示例:

在此处输入图片说明

I could put this in and it will click on it all the time: 我可以把它放进去,它会一直点击它:

driver.findElement(By.xpath("//*[text()='midrfrate']"]"));

However if i do something like this it cant find it: 但是,如果我做这样的事情,它找不到它:

 driver.findElement(By.xpath(".//*[(text(),"+client+"]")).click();
  driver.findElement(By.xpath("//[contains(text(),"+client+")]")).click();

Please help me I have tried the following with no success: How to use variables in XPath? 请帮助我,我尝试了以下失败的尝试: 如何在XPath中使用变量?

The string ".//*[(text(), " + client + "]" is equals to ".//*[(text(), midrfrate]" 字符串".//*[(text(), " + client + "]"等于".//*[(text(), midrfrate]"

You need to put the variable in apostrophes 您需要将变量放在撇号中

".//*[contains(text(), '" + client + "']"

Your xpath there: 您的xpath:

driver.findElement(By.xpath("//*[contains(text(),"+client+")]")).click(); :

  1. Should contain apostrophes 应包含撇号

  2. Text in contains block should contain exact String (you have trailing whitespace in client variable 'midrfrate '). 包含块中的文本应包含确切的字符串(客户端变量“ midrfrate”中具有尾随空格)。

Final xpath is: 最终的xpath是:

driver.findElement(By.xpath("//*[contains(text(),'"+client.trim()+"')]")).click();

where trim() method removes leading and trailing whitespace from client variable. 其中trim()方法从客户端变量中删除前导和尾随空格。

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

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