2012-01-13 95 views
4

頁面具有超鏈接圖像,並且該超鏈接具有target =「_ blank」,並且每當我按下該圖像加載新的Firefox並且該超鏈接被重定向到該新的Firefox Web ,該網頁。 是possilble刪除或更改目標=「_空白」的超鏈接,bcause我想在相同的webdriver加載網頁webdriver target =「_ blank」

WebDriver driver = new FirefoxDriver(); 

    driver.get("http://www.page.eu/"); 
    WebElement submit; 
    submit = driver.findElement(By.xpath("//img[@alt='page']")); 
    submit.click(); 

該超鏈接有目標=「_空白」 我需要通過某種方式改變該目標webdriver + javascript可能還是什麼? 有可能嗎?

編輯

感謝的建議,但仍然是這個問題 我試圖使像Grooveek說,但沒有改變

WebElement labels2 = driver.findElement(By.xpath("//a[@href='http://tahtpage.net']")); 
    WebElement aa = (WebElement) ((JavascriptExecutor) driver).executeScript("labels2.setAttribute('target','_self')",labels2); 
    aa.click(); 

我有一個錯誤 org.openqa。 selenium.WebDriverException:null(警告:服務器沒有提供任何堆棧跟蹤信息)

我,我不擅長javascrit,所以我認爲這是問題,即執行

+0

+1,因爲這個問題讓我想到,一些開箱。但似乎仍然如此,我所擁有的有限知識不足以幫助解決這個問題。我的願望,希望你成功。問候 – 2012-01-13 15:15:53

回答

1

嘗試以下操作:

WebElement labels2 = driver.findElement(By.xpath("//a[@href='http://tahtpage.net']")); 
WebElement aa = (WebElement) ((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('target','_self')",labels2); 
aa.click(); 

你,因爲你是在你的JavaScript,這並不在這方面存在的使用labels2得到一個空例外。通過將其更改爲arguments[0] Selenium將採用labels2參數並在JavaScript中適當引用它。

+0

非常感謝m8,這個作品完美無缺 – Palaima 2012-01-13 19:12:34

0

你爲什麼不使用:

target="_self" 
+0

超鏈接有目標=「_空白」 我需要改變目標以某種方式使用webdriver + javascript也許或什麼? 有可能嗎? – Palaima 2012-01-13 12:58:05

+0

hv看起來這可能會幫助你... http:// stackoverflow。com/questions/2423064 /動態添加目標屬性到一個預先存在的錨點標籤集合 – 2012-01-13 13:02:51

1

窗口評估的JavaScript將幫助你抑制目標=空白鏈接 下面是來自Webdriver docs

List<WebElement> labels = driver.findElements(By.tagName("label")); 
List<WebElement> inputs = (List<WebElement>) ((JavascriptExecutor)driver).executeScript(
    "var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" + 
    "inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;", labels); 

的例子適應它來修改DOM扔目標=「_空白鏈接」

2

而不是點擊圖片,你可以直接前往該URL的鏈接:

WebElement link = (driver.findElement(By.xpath("//img[@alt='page']/parent::*")); 
String href = link.getAttribute("href"); 
driver.get(href); 
+0

該URL是受保護的,它只能通過點擊圖片進入,只能進入 – Palaima 2012-01-13 13:20:17

+0

它是如何被保護的?一個URL是一個URL。你有沒有嘗試上面的代碼? – 2012-01-13 15:37:18

1

爲什麼你不想要使用SWITCHTO()窗口?

+0

你能解釋多一點,因爲我認爲我傾倒在硒和網絡驅動器... – Palaima 2012-01-13 15:04:02

+0

你點擊圖像 - 在這裏,我們是,新窗口=)它現在有ID 1。我們需要切換到第一個窗口。它有ID 0。現在我們可以寫出:List handle = new List (WebDriver.WindowHandles); TestFramework.WebDriver.SwitchTo()。Window(handle [windowNumber]); – 2012-01-13 21:20:34

+0

或者我們可以用圖像關閉我們的第二個窗口 - WebDriver.SwitchTo()。Window(windowName); WebDriver.Close(); – 2012-01-13 21:24:25

1

我想你應該使用simeon sinichkin建議的SwitchTo()。窗口。但是,我不喜歡他的例子。這裏是一個簡單的例子。

driver.Navigate().GoToUrl(baseURL); 
//Get the Main Window Handle 
var baseWindow = driver.CurrentWindowHandle; 
// navigate to another window (_blank) 
driver.FindElement(By.Id("btn_help")).Click(); 
Thread.Sleep(2000); 
//switch back to Main Window 
driver.SwitchTo().Window(baseWindow); 

希望幫助

相關問題