是否可以在不使用 Selenium Webdriver Java 中的 driver.switchTo().frame(“frameName") 的情况下切换到框架中的元素? [英] Is it possible to switch to an element in a frame without using driver.switchTo().frame("frameName") in Selenium Webdriver Java?
问题描述
我有多个嵌套框架,我需要访问这些框架下的元素.由于这些框架是动态的,我无法访问这些元素.
是否可以在不使用 driver.switchTo().frame()
的情况下访问元素?
喜欢直接使用 xpath 还是 jquery、javascript 或其他任何东西?欢迎任何建议
简单来说,
不,如果不切换到预期的,即不使用,就不可能访问元素>
driver.switchTo().frame()
要切换到预期的框架,您必须使用以下任一方法:
切换帧名称:
driver.switchTo().frame("frame_name");
切换帧 ID:
driver.switchTo().frame("frame_id");
切换帧索引:
driver.switchTo().frame(1);
通过WebElement切换:
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@attribute='value']")));
切换到父框架:
driver.switchTo().parentFrame();
切换到默认内容:
driver.switchTo().defaultContent();
但根据最佳实践,您应该始终为所需的可用框架和切换到它引入WebDriverWait,如下所示:>
切换帧名称:
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("frame_name")));
切换帧 ID:
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("frame_id")));
通过Frame cssSelector切换:
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("frame_cssSelector")));
通过Frame xpath切换:
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("frame_xpath")));
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 <iframe>
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")));
这篇关于是否可以在不使用 Selenium Webdriver Java 中的 driver.switchTo().frame(“frameName") 的情况下切换到框架中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!