![](/img/trans.png)
[英]How to switch to the new browser window, which opens after click on the button?
[英]How to click a link within an iFrame which opens a new tab and switch to it
我正在做一個項目,其中包括單擊鏈接,並且應該使用webdriver在新選項卡中打開,問題是
假定的鏈接包含在iFrame中,因此shift+click
無法正常工作
private void openInNewTabAndSwitch(WebElement linkElement) { // logic of opening in new tab goes here... Actions newTab = new Actions(driver); newTab.keyDown(Keys.SHIFT).click(linkElement).keyUp(Keys.SHIFT).build().perform(); Set<String> windowSet = driver.getWindowHandles(); driver.switchTo().window((String) windowSet.toArray()[1]); }
我找不到href
屬性,因為某些javascript函數正在使用某些onClick()
打開它
<a onclick="javascript:LinkOccam (this, 'opportunity');">Mednomics Proposition</a>
問題:-它只是在同一選項卡中打開所需的頁面。
現在,我找不到與此相關的任何信息,請幫助..!
我正在使用Windows 7,Java 8,ChromeDriver的其他相關信息
當您單擊打開新TAB的WebElement時 ,無需擔心WebElement是在頂級瀏覽上下文中還是在iFrame中 。 另外,在切換TAB時,請誘導WebDriverWait並進行相應的切換。 為此,可以使用以下代碼塊:
private void openInNewTabAndSwitch(WebElement linkElement)
{
String parentTab = driver.getWindowHandle();
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).click(linkElement).keyUp(Keys.CONTROL).build().perform();
WebDriverWait wait = new WebDriverWait(driver,5);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> windowSet = driver.getWindowHandles();
for(String tab:windowSet)
{
if(!parentTab.equalsIgnoreCase(tab))
{
driver.switchTo().window(tab);
//do your work in the newly opened TAB
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.