java中的switch to,是否可以在Selenium Webdriver Java中使用driver.switchTo()。frame(" frameName")而切换到框架...

I have a multiple nested frames and I need to access the elements under these. As these frames are dynamic I am not able to access these elements.

Is it possible for me to access the elements without using driver.switchTo().frame()

Like using the xpath directly or jquery, javascript or anything?

Any suggestions are welcome

解决方案

In simple words,

No, it wouldn't be possible to access the elements without switching into the intended i.e. without using driver.switchTo().frame()

To switch to the intended frame you have to use either of the following :

Switch through Frame Name:

driver.switchTo().frame("frame_name");

Switch through Frame ID:

driver.switchTo().frame("frame_id");

Switch through Frame Index:

driver.switchTo().frame(1);

Switch through WebElement:

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@attribute='value']")));

Switch to Parent Frame:

driver.switchTo().parentFrame();

Switch to Default Content:

driver.switchTo().defaultContent();

But as per best practices you should always induce WebDriverWait for the desired Frame To Be Available And Switch To It as follows:

Switch through Frame Name:

new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("frame_name")));

Switch through Frame ID:

new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("frame_id")));

Switch through Frame cssSelector:

new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("frame_cssSelector")));

Switch through Frame xpath:

new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("frame_xpath")));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值