Selenium自动化等待处理代码参考

Selenium自动化等待

隐式等待

python版本

self.drvier.implicitly_wait(30)


java版本
//隐式等待调用方式,设置等待时间为30秒
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);




python版本


# 导入显式等待
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_condittions
...
# 设置10秒的最大等待时间,等待(By.TAG_NAME,"title")这个元素被点击
WebDriverWait(driver,10).until(
    expected_conditions eleniumt_to_be_clickable((By.TAG_NAME,"title"))
)

Java版本

import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
...
//设置10秒的最大等待时间,等待(By.tag_NAME,"title")这个元素被点击
WebDriverWait wait = new WebDriverWait(driver,10);
wait.unitl(ExpectedConditions.elementToBClickable(By.tagName("title")));


等待某个方法执行完并返回true

python版本

def ceshiren():
     # 定义一个方法
     def wait_ele_for(driver):
     # 将找到的元素个数赋值给eles
     eles = driver.find_enements(By.XPATH,'//*[@id="site-test-logo"]')
     #返回结果
     returun len(eles)>0
     driver = webdriver.Chrome()
     driver.get('https://ceshiren.com')
     # 等待10秒,直到wait_ele_for返回true
     WebDriverWait(driver,10).until(wait_ele_for)
 



java版本

void ceshiren(){
      webDriver = new ChromeDriver();
      webDriver.get("https://ceshiren.com");
      //等待10秒,直到wait_ele_for返回true
      new WebDriverWait(webDriver,10).until((ExpectredCondition<Boolean>) size ->waitEleFor());
}
//定义方法
Boolean waitEleFor(){
    // 将找到的元素个数赋值给 elements
    List<WebElement>element = webDriver.findElements(By.xpath("//*[@id='sire-text-logo']"));
    return elements.size()>0
}

WebDriverWait wait = new WebDriverWait(dr,10)
wait.until(new ExpectedCondition<WebElement>(){
            @Override
            public WebElement apply(WebDriver d) {
                return d.findElement(By.id("b"));
            }}).click()
 
 
 3.强制等待

sleep(3000)




import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

public class WebDriverWaitTest{
    private static ChromeDriver driver;
    @BeforeAll
    public static void setUp(){
        System.setProperty(
              "webdrive.chrome.driver",
              "/driver/chrome95/chromedriver"
        );
        driver = new ChromeDrive();
        driver.manage().timeout().implicitlyWait(60,TimeUnit.SECONDS);
    }
    @AfterAll
    public static void teardown(){
        driver.quit();
    }
    @Test
    public void waitTest(){
         driver.get("https://ceshiren.com/");
         By locator = By.linkTest("开源项目");
         //加入显式等待
         WebDriverWait wait = new WebDriverWait(driver,10);
         Wait.until(ExpectedConditions.elementToBeClickable(locator));
         //点击开源项目
         driver.findElement(locator).click();
         }
}
 
posted @ 2023-03-10 23:46  桃花雪  阅读(49)  评论(0编辑  收藏  举报