2016-12-21 121 views
2

我試圖點擊在類別屬性爲「modalIframe」的iframe中放置的SIGN IN鏈接。過去兩天我一直在努力尋找解決方案,但無法做到。任何幫助將非常感激。無法使用Selenium webdriver切換到iframe

代碼如下

public class Datereader 
{ 

    public static void main(String[] args) 
    { 
     System.setProperty("webdriver.gecko.driver","C:\\Users\\Madankumar\\Desktop\\Gecko Driver\\geckodriver.exe"); 
     WebDriver driver=new FirefoxDriver(); 
     driver.get("https://www.redbus.in/"); 
     driver.manage().window().maximize(); 
     driver.findElement(By.xpath(".//div[@class='icon-down icon ich dib']")).click(); 
     driver.findElement(By.id("signInLink")).click();  
     driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); 
     WebElement iframeElement=driver.findElement(By.xpath("//*[@class='modalIframe']")); 
     driver.switchTo().frame(iframeElement); 
     driver.findElement(By.linkText("sign in")).click(); 


    } 
} 

在運行的代碼我得到以下錯誤:

JavaScript warning: https://cdn-jp.gsecondscreen.com/static/tac.min.js , line 3: unreachable code after return statement

+0

可以共享完整的錯誤日誌 – Giri

回答

0

在探索HTML和嘗試不同的XPath找到的元素,它被觀察到,是3 elements目前與相同的元素屬性。因此,爲了使其具有獨特性,請從GooglePlus Signup元素開始構建相對XPATH,然後找到相對於該元素的sign in鏈接。

試試下面的代碼:

WebDriver driver=new FirefoxDriver(); 
    driver.get("https://www.redbus.in/"); 
    driver.manage().window().maximize(); 
    driver.findElement(By.xpath(".//div[@class='icon-down icon ich dib']")).click(); 
    driver.findElement(By.id("signInLink")).click();  
    driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); 
    WebElement iframeElement=driver.findElement(By.xpath("//*[@class='modalIframe']")); 
    driver.switchTo().frame(iframeElement); 
    WebElement elem = driver.findElement(By.xpath("//div[@id='googlePlusBtn1']/following-sibling::div/span/a")); 
    System.out.println("element " + elem); 
    Thread.sleep(1000); // without this line, observed that click is not resulting in displaying the Login form, though selenium did not throw any error (means, click did not result in Login form). you can alternatively try out with WebDriverWait. this is trial and error. if it is working for you without sleep, you can remove this line. 
    elem.click(); 
    Thread.sleep(5000); // can remove sleep. kept only for visual confirmaiton to check whether Login form is displayed, as there are no steps further after this in the code. 
+0

唯一代碼的答案都望而卻步。你應該解釋OP代碼不工作的原因以及你的解決方法。也有沒有辦法可以避免Thread.sleep()?這通常被認爲是很不好的做法,除非絕對必要 – ExoticChimp

+0

@ExoticChimp編輯了答案,解釋我已經做了解決問題的事情。感謝您的好建議。 –

+0

謝謝@Naveen。該代碼在刪除Thread.sleep()時與WebDriverWait一起工作。此外,對於SIGN IN元素,我也嘗試過使用linkText,但是即使沒有解決。 – Madan

1

使用下面的代碼來處理IFRAME。

WebElement iframe = driver.findElement(By.tagName("iframe")); 
driver.switchTo().frame(iframe);   //Move inside to the frame. 
WebElement body = driver.findElement(By.tagName("body")); 
body.click(); 
driver.findElement(By.linkText("sign in")).click(); 
driver.switchTo().defaultContent();  //Move outside to the frame. 
+0

非常感謝。這對我有效。一直在我的頭上撞牆幾個小時。似乎身體單擊是答案 – mark1234

+0

偉大.. !!!如果問題解決了,請將此答案標記爲「已接受」,因爲這對其他用戶也有幫助。 –

+0

我向上投了票,但由於某種原因沒有勾號 – mark1234

相關問題