简体   繁体   English

我想使用硒使用python选择框架

[英]I would like to use selenium to select a frame using python

I can successfully login to web pages but I need to click/select the frame. 我可以成功登录网页,但是我需要单击/选择框架。 here is my code so far: 到目前为止,这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("URL")
Login = driver.find_element_by_name('login')
Login.send_keys("USER NAME")
Passwd = driver.find_element_by_name('password')
Passwd.send_keys("PSWD")
driver.find_element_by_name('login_submit').click()
driver.switch_to_default_content()
driver.find_element_by_link_text('Test')

Here is the frames source code. 这是框架源代码。

<div id="Test_topic" ><a href="<link>">Test Spec</a>

Try performing click() operation via jQuery or JavaScript using execute_script method of selenium. 尝试使用selenium的execute_script方法通过jQuery或JavaScript执行click()操作。 Below is the sample code that will perform the click operation. 下面是将执行单击操作的示例代码。

driver.execute_script('elem = document.getElementById("Test_topic"); a = elem.getElementsByTagName("a")[0]; a.click()')

execute_script(script, *args) execute_script(脚本,* args)

Synchronously Executes JavaScript in the current window/frame. 在当前窗口/框架中同步执行JavaScript。

Args: script: The JavaScript to execute. Args:脚本:要执行的JavaScript。 *args: Any applicable arguments for your JavaScript. * args:JavaScript的所有适用参数。

Usage: driver.execute_script('document.title') 用法: driver.execute_script('document.title')

The link text is not "Test" but "Test Spec". 链接文本不是“测试”,而是“测试规范”。 Try this: 尝试这个:

driver.find_element_by_link_text('Test Spec')

Also, since there is one, it would be better if you find the element by it's id: 另外,由于有一个,因此最好通过其ID查找元素:

driver.find_element_by_link_id('Test_topic').find_element_by_tag_name('a').click()

I personally prefer to use css: 我个人更喜欢使用CSS:

driver.find_element_by_link_css_selector('div#Test_topic a').click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM