2024-05-14 10:36:12 发布
网友
我正在尝试选择一个驻留在iframe中的元素,也可能是其他iframe中的元素。在
是否可以在(python)selenium中的某个(子)iframe中选择一个元素,而不事先选择iframe?有没有一种方法可以在每个iframe上“循环”并检查在哪里可以找到我的元素。。。?在
如何在case元素、html内容和iframe中做到这一点呢。。。?在
不,不切换到相应的iframe到Selenium内的任何WebElement进行交互。在
iframe
Selenium
WebElement
加载页面时,Selenium的焦点默认保持在Top Window上。Top Window包含另一个iframes和framesets。因此,当我们需要与iframe中的WebElement交互时,我们必须通过下面提到的方法之一切换到相应的{}:
Top Window
iframes
framesets
我们可以通过三种方式切换到帧。在
按帧名称:
iframe的Name属性,通过它我们可以切换到它。在
Name
示例:
driver.switch_to.frame("iframe_name")
按帧ID:
iframe的ID属性,通过它我们可以切换到它。在
ID
按帧索引:
假设页面中有10个框架,我们可以使用索引切换到iframe。在
driver.switch_to.frame(0) driver.switch_to.frame(1)
切换回主框架:
我们可以使用default_content()或parent_frame()
default_content()
parent_frame()
driver.switch_to.default_content() driver.switch_to.parent_frame()
切换帧的一种更好的方法是,将WebDriverWait设置为expected_conditions以获得预期帧的可用性,如下所示:
WebDriverWait
expected_conditions
通过Frame ID:
Frame ID
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.ID,"id_of_iframe"))
通过Frame Name:
Frame Name
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.NAME,"name_of_iframe"))
通过Frame Xpath:
Frame Xpath
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.XPATH,"xpath_of_iframe"))
通过Frame CSS:
Frame CSS
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.CSS_SELECTOR,"css_of_iframe"))
编写自己的递归查找器应该很容易。抱歉,我不知道python,但在Java中它可能是:
public void findInAllFrames(WebElement e, String targetIdStr) { List<WebElement> l = e.findElements(By.tagName("iframe")); for(int inx=0; inx<l.size(); inx++) { List<WebElement> targets = l.get(inx).findElements(By.id(targetIdStr)); if(targets.size()>0) { // Do something with your targets } findInAllFrames(l.get(inx), targetIdStr); } }
不,不切换到相应的
iframe
到Selenium
内的任何WebElement
进行交互。在原因:
加载页面时,}:
Selenium
的焦点默认保持在Top Window
上。Top Window
包含另一个iframes
和framesets
。因此,当我们需要与iframe中的WebElement
交互时,我们必须通过下面提到的方法之一切换到相应的{帧切换方法:
我们可以通过三种方式切换到帧。在
按帧名称:
iframe的
Name
属性,通过它我们可以切换到它。在示例:
按帧ID:
iframe的
ID
属性,通过它我们可以切换到它。在示例:
^{pr2}$按帧索引:
假设页面中有10个框架,我们可以使用索引切换到iframe。在
示例:
切换回主框架:
我们可以使用
default_content()
或parent_frame()
示例:
切换帧的更好方法:
切换帧的一种更好的方法是,将
WebDriverWait
设置为expected_conditions
以获得预期帧的可用性,如下所示:通过
Frame ID
:通过
Frame Name
:通过
Frame Xpath
:通过
Frame CSS
:编写自己的递归查找器应该很容易。抱歉,我不知道python,但在Java中它可能是:
相关问题 更多 >
编程相关推荐