我正在尝试点击SIGN IN链接,该链接位于iframe中,类属性为“modalIframe”。过去两天我一直在努力寻找解决方案,但却无法解决。任何帮助都会非常感激。
代码如下
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警告:https://cdn-jp.gsecondscreen.com/static/tac.min.js,第3行:返回语句后无法访问的代码
答案 0 :(得分: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.
答案 1 :(得分: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.