您的位置:首頁技術文章
文章詳情頁

javascript - Java selenium 中使用click()方法點擊超鏈接無效 用window.open() 也打不開頁面

瀏覽:204日期:2024-03-31 18:22:23

問題描述

用 a.click(); 無效 明明其他時候都可以的

而且就算那我獲取了href 在用js打開也沒反應 這是為什么呢 ((JavascriptExecutor)driver).executeScript('window.open(’'+href+'’)'); ((JavascriptExecutor)driver).executeScript('alert(’'+href+'’)'); alert能彈出

但是window.open()就沒反應 在網頁中控制臺直接敲都好使

問題解答

回答1:

window.open()打開的是新標簽,你需要切換handles。給你兩個方法參考:

public static void changeWindow(WebDriver driver){// 獲取當前頁面句柄String handle = driver.getWindowHandle();// 獲取所有頁面的句柄,并循環判斷不是當前的句柄,就做選取switchTo()for (String handles : driver.getWindowHandles()) { if (handles.equals(handle))continue; driver.switchTo().window(handles);} } public static void changeWindowTo(WebDriver driver,String handle){for (String tmp : driver.getWindowHandles()) { if (tmp.equals(handle)){driver.switchTo().window(handle);break; }} }

至于click不了,可能是因為那個元素不顯式可見,或者需要聚焦,或你用Selenium操作不當,不過建議可以嘗試js click

標簽: JavaScript
相關文章:
国产综合久久一区二区三区