所以,我使用了这个代码(请注意评论部分)WebDriverWait。它运作得很好。不久前,我可能会将selenium-standalone-driver更新到3.4.0(但我不确定这个,因为我已经设置了这个版本)。
WebDriverWait wait = new WebDriverWait(driver, 10);
//FluentWait wait = new FluentWait <>(driver).withTimeout(10,TimeUnit.SECONDS).pollingEvery(5, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
WebElement searchStroke = driver.findElement(searchField);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.className("leaflet-zoom-animated"))));
searchStroke.sendKeys(positive);
searchStroke.sendKeys(Keys.ENTER);
Thread.sleep(1000);
searchStroke.clear();
searchStroke.sendKeys(Keys.TAB);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(output)));
现在我收到此消息(javac):
Error:(35, 13) java: method until in class org.openqa.selenium.support.ui.FluentWait<T> cannot be applied to given types;
required: java.util.function.Function<? super org.openqa.selenium.WebDriver,V>
found: org.openqa.selenium.support.ui.ExpectedCondition<org.openqa.selenium.WebElement>
reason: cannot infer type-variable(s) V
(argument mismatch; org.openqa.selenium.support.ui.ExpectedCondition<org.openqa.selenium.WebElement> cannot be converted to java.util.function.Function<? super org.openqa.selenium.WebDriver,V>)
依赖项:
<properties>
<java.version>1.8</java.version>
<allure.version>2.1.1</allure.version>
<aspectj.version>1.8.10</aspectj.version>
<compiler.plugin.version>3.6.1</compiler.plugin.version>
<allure.results.directory>
${project.build.directory}/allure-results
</allure.results.directory>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<inherited>true</inherited>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<argLine>
-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
</argLine>
<properties>
<property>
<name>listener</name>
<value>ru.yandex.qatools.allure.testng.AllureTestListener</value>
</property>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
</properties>
<systemProperties>
<property>
<name>allure.results.directory</name>
<value>${allure.results.directory}</value>
</property>
<property>
<name>allure.link.issue.pattern</name>
<value>${issues.tracker}</value>
</property>
<property>
<name>allure.link.tms.pattern</name>
<value>${tms}</value>
</property>
</systemProperties>
<suiteXmlFiles>
<suiteXmlFile>../testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>2.30</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<!--<scope>compile</scope>-->
</dependency>
<dependency>
<groupId>ru.yandex.qatools.allure</groupId>
<artifactId>allure-testng-adaptor</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
</dependencies>
</dependencyManagement>
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.testng.Assert.assertTrue;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
我只有两个问题:
1)为什么我在使用WebDriverWait时会向我显示“FluentWait”类?
2)我尝试过其他一些输入(在代码中注释);有人说JRE更新有帮助(我不确定);我无法理解这背后的原因。就像,一切都很完美,我没有太大改变(我试图添加Allure所以结果我的libs和依赖关系被删除。我必须恢复它。)
所以,有人可以看透这个问题,或者我只是愚蠢(ofc),应该使用另一种方法。但是我不会过多地修改代码,因为WebDriverWait易于使用且可以理解。
询问您需要的任何(几乎任何)信息。非常感谢。