要求:默认情况下,在主窗口上搜索网络元素,如果找到则执行操作,否则在iframe中搜索网络元素并执行所需的操作
selenium3.141
'''
WebElement el = driver.findElement(By.xpath("//*[contains(text(),'here')]"));
boolean displayFlag = el.isDisplayed();
if(displayFlag == true)
{
sysout("element available in main window")
el.click();
}
else
{
for(int f=0;f<10;f++)
{
sysout("element available in frameset")
switchToFrame(frameName[f]);
el.click();
System.out.println("Webelement not displayed");
}
}
'''
我的脚本本身在第一行失败。它正在尝试在主窗口中查找元素,但该元素实际上在iframe中可用。
但要求是先在主窗口中搜索,然后再导航至iframe。如何处理这种用例?
有什么建议会有所帮助吗?谢谢。
是的,如果主窗口中不存在该元素,则可以编写一个循环遍历所有iframe。 Java实现:
if (driver.findElements(By.xpath("xpath goes here").size()==0){
int size = driver.findElements(By.tagName("iframe")).size();
for(int iFrameCounter=0; iFrameCounter<=size; iFrameCounter++){
driver.switchTo().frame(iFrameCounter);
if (driver.findElements(By.xpath("xpath goes here").size()>0){
System.out.println("found the element in iframe:" + Integer.toString(iFrameCounter));
// perform the actions on element here
}
driver.switchTo().defaultContent();
}
}
Python实现
# switching to parent window - added this to make sure always we check on the parent window first
driver.switch_to.default_content()
# check if the elment present in the parent window
if (len(driver.finds_element_by_xpath("xpath goes here"))==0):
# get the number of iframes
iframes = driver.find_elements_by_tag_name("iframe")
# iterate through all iframes to find out which iframe the required element
for iFrameNumber in iframes:
# switching to iframe (based on counter)
driver.switch_to.frame(iFrameNumber+1)
# check if the element present in the iframe
if len(driver.finds_element_by_xpath("xpath goes here")) > 0:
print("found element in iframe :" + str(iFrameNumber+1))
# perform the operation here
driver.switch_to.default_content()
我得到了如下html代码片段: 该元素在html中唯一唯一的标识是属性,因此我想使用类似以下的Python selenium方法来定位它: 我已经查看了selenium(python)文档以查找元素,但没有找到如何通过attr来定位此元素的线索。在python selenium中是否有明确的方法来定位这个元素?
最近我接受了一次采访,他们问我一个“搜索”问题。问题是: 假设存在一个(正)整数数组,其中每个元素与其相邻元素相比要么是,要么是。 例: 现在搜索并返回其位置。 我给出了这样的答案: 将这些值存储在临时数组中,对它们进行排序,然后应用二进制搜索。 如果找到元素,则返回其在临时数组中的位置 (如果数字出现两次,则返回第一次出现的数字) 但是,他们似乎对这个答案不满意。 正确的答案是什么?
我正在关注一个名为“在C#中使用SQLite”的YouTube教程。 当他在NuGet/Browse中输入“sqlite”时,他会得到这些结果 但是当我打字的时候,我什么也没有得到 我在谷歌上搜索时什么也找不到。如果有人能帮忙,我将感激不尽。
问题内容: 我有一个这样的html代码段: html中此元素的唯一唯一标识是attribute ,因此我想使用 某种 Pythonselenium 方法 来定位它,如下所示: 我已经检查了selenium(python)文档以查找元素, 但是没有获得关于如何通过attr 定位此elem的线索。有没有在pythonselenium中找到该元素的明确方法? 问题答案: 您可以 通过xpath 获取它并
我有两个清单,例如: 列表1:只有一个元素 列表2:有1000个对象 注意:矩阵是字符串集合(例如:abc,定义,ghi) 问题: 在列表1中只有1个元素,而在列表2中有1000个。我需要检查所有这1000个元素才能找到1个元素吗? 有没有更好的办法?
我需要对以搜索词开头的搜索结果进行优先级排序。我用了“匹配短语前缀”。我用了“匹配短语前缀”。但它不起作用。 细节: > 质疑 结果