web - java selenium 設(shè)置顯式等待 沒(méi)有效果
問(wèn)題描述
最近使用的selenium,做個(gè)簡(jiǎn)單的自動(dòng)化工具,但是測(cè)試的時(shí)候感覺(jué) WebDriverWait wait = new WebDriverWait(driver,20);沒(méi)啥效果啊,在我進(jìn)入郵箱之后wait.until(ExpectedConditions.presenceOfElementLocated(By.id('_mail_component_68_68'))).click(); //打開(kāi)收件箱
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath('//*[@sign=’letter’][1]'))).click(); 這兩段代碼明明頁(yè)面已經(jīng)有這個(gè)元素了,為啥還要等一段時(shí)間才進(jìn)行操作,而且等待是時(shí)間也不是固定的,是我電腦問(wèn)題,很頭疼啊,演示的時(shí)候非常尷尬的等好長(zhǎng)時(shí)間。 selenium 用的3.4 火狐用的5.3
static {System.setProperty('webdriver.firefox.marionette','C:Program Files (x86)Mozilla Firefoxgeckodriver.exe');} //火狐瀏覽器驅(qū)動(dòng)
static String url='http://freemail.163.com/';//瀏覽器打開(kāi)的地址
public String openEmail(Stream<String> userNameSream,Stream<String> passwordStream) throws Exception {
long startTime=System.currentTimeMillis(); //獲取開(kāi)始時(shí)間 WebDriver driver = new FirefoxDriver(); //啟動(dòng)瀏覽器driver.manage().window().maximize(); //最大化瀏覽器WebDriverWait wait = new WebDriverWait(driver,20);driver.get(url);//打開(kāi)連接driver.switchTo().frame('x-URS-iframe');//定位到iframe框架中userNameSream.forEach(e -> driver.findElement(By.name('email')).sendKeys(e));//輸入賬號(hào)passwordStream.forEach(p -> driver.findElement(By.name('password')).sendKeys(p));//輸入賬號(hào)driver.findElement(By.id('dologin')).click();//點(diǎn)擊登錄Thread.sleep(3000);if(driver.getTitle().contains('網(wǎng)易郵箱')){ wait.until(ExpectedConditions.presenceOfElementLocated(By.id('_mail_component_68_68'))).click(); //打開(kāi)收件箱 wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath('//*[@sign=’letter’][1]'))).click(); return '成功,從程序開(kāi)始到打開(kāi)第一封郵件用時(shí)'+(System.currentTimeMillis()-startTime)/1000+'秒';}else{ try { WebElement loginMsg = driver.findElement(By.className('ferrorhead')); return loginMsg.getText(); }catch(Exception e){return '出現(xiàn)其他問(wèn)題'; }}
問(wèn)題解答
回答1:WebDriverWait是全部加載完才會(huì)執(zhí)行click
回答2:不知道為什么 但是解決了 我用的火狐 換谷歌之后就變得很快了
相關(guān)文章:
1. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問(wèn)題2. css3 - 圖片等比例縮放3. html - css3中多列高度 統(tǒng)一4. javascript - 使用angular 的ui-sref 中出現(xiàn)了中文參數(shù),點(diǎn)擊跳轉(zhuǎn)后瀏覽器的地址欄里出現(xiàn)轉(zhuǎn)義后的%AE....%a%45. css3 - 如何將網(wǎng)頁(yè)CSS背景圖高斯模糊且全屏顯示6. html5 - 這個(gè)代碼顯示功能如何實(shí)現(xiàn)?7. css3 - animation屬性,safari瀏覽器不支持相關(guān)效果8. javascript - 求賜教:網(wǎng)易郵箱Web端模擬登錄看信的加密參數(shù)_ntes_nnid、_ntes_nuid9. css - jq有無(wú)現(xiàn)成函數(shù)改變r(jià)otateX/Y的deg10. javascript - QWebEngineView 如何爬 angular 的動(dòng)態(tài)數(shù)據(jù)?
