无法使用 Web 驱动程序遍历框架 java
Unable to traverse through a frame using web driver java
我正在尝试获取第二帧内的元素here
但我收到错误消息,指出该元素不存在或
Unable to locate element: {"method":"xpath","selector":"//frameset/frame[2]//a"}
我已经尝试了所有方法,但它对我不起作用,这是我的代码
WebDriver driver = new ChromeDriver();
driver.get("https://dps.psx.com.pk/");
//switch to the mainFrame
WebElement mainFrame = driver.findElement(By.xpath("//frameset/frame[2]//a"));
driver.switchTo().frame(mainFrame);
List<WebElement> childs = mainFrame.findElements(By.xpath(".//*"));
for(WebElement child : childs) {
System.out.println(child);
}
我也试过等待元素加载,然后尝试访问框架内的元素,但仍然出现相同的错误。
frame
定位器是 //frameset/frame[2]
,//a
是对 frame
.
的向下钻取
也可以直接使用name
属性进行切换。 switchTo().frame()
可以接收 id
、name
或 WebElement
作为参数
driver.switchTo().frame("mainFrame");
如果加载帧需要时间,您可以使用显式等待和 ExpectedCondition
frameToBeAvailableAndSwitchToIt
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//frameset/frame[2]")));
根据您分享的 HTML 要遍历到所需的 frame
您必须:
- 诱导 WebDriverWait 所需的框架可用并切换到它。
诱导 WebDriverWait 使所需的 元素可点击 并且您可以使用以下解决方案:
使用name
:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("mainFrame")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.name("element_name"))).click();
使用xpath
:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//frame[@name='mainFrame' and contains(@src,'index1')]")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("element_xpath"))).click();
我正在尝试获取第二帧内的元素here
但我收到错误消息,指出该元素不存在或
Unable to locate element: {"method":"xpath","selector":"//frameset/frame[2]//a"}
我已经尝试了所有方法,但它对我不起作用,这是我的代码
WebDriver driver = new ChromeDriver();
driver.get("https://dps.psx.com.pk/");
//switch to the mainFrame
WebElement mainFrame = driver.findElement(By.xpath("//frameset/frame[2]//a"));
driver.switchTo().frame(mainFrame);
List<WebElement> childs = mainFrame.findElements(By.xpath(".//*"));
for(WebElement child : childs) {
System.out.println(child);
}
我也试过等待元素加载,然后尝试访问框架内的元素,但仍然出现相同的错误。
frame
定位器是 //frameset/frame[2]
,//a
是对 frame
.
也可以直接使用name
属性进行切换。 switchTo().frame()
可以接收 id
、name
或 WebElement
作为参数
driver.switchTo().frame("mainFrame");
如果加载帧需要时间,您可以使用显式等待和 ExpectedCondition
frameToBeAvailableAndSwitchToIt
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//frameset/frame[2]")));
根据您分享的 HTML 要遍历到所需的 frame
您必须:
- 诱导 WebDriverWait 所需的框架可用并切换到它。
诱导 WebDriverWait 使所需的 元素可点击 并且您可以使用以下解决方案:
使用
name
:new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("mainFrame"))); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.name("element_name"))).click();
使用
xpath
:new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//frame[@name='mainFrame' and contains(@src,'index1')]"))); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("element_xpath"))).click();