gpt4 book ai didi

python - Selenium - 如何调试 iframe > 输入字段的超时?

转载 作者:太空宇宙 更新时间:2023-11-03 18:11:24 26 4
gpt4 key购买 nike

我一直在为 Instagram 网络登录编写 Selenium WebDriver 脚本。我想我切换到了适当的 iframe,但 WebDriver 在定位用户输入字段时一直超时。

来自 Instagram 网站的相关来源:

https://instagram.com/accounts/login/
<iframe class="hiFrame" data-reactid=".0.0.0.1.0.1.0.0.$frame" src="https://instagram.com/accounts/login/ajax/?targetOrigin=https%3A%2F%2Finstagram.com" scrolling="no" seamless="">
<!DOCTYPE html>
<html class="hl-en not-logged-in " lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<body class="LoginFormChrome ">
<div class="LoginFormPage" data-reactid=".0">
<form data-reactid=".0.0">
<p class="lfField" data-reactid=".0.0.0">
<label class="lfFieldLabel" data-reactid=".0.0.0.0">
<input class="lfFieldInput" type="text" data-reactid=".0.0.0.1" value="" autocorrect="false" autocapitalize="false" maxlength="30" name="username">
</p>

来自 Selenium 脚本的来源:

login_url = 'https://instagram.com/accounts/login/'
profile_url = '<path_firefix_profile>'
user = '<user_name>'
#login
my_profile = FirefoxProfile(profile_url)
self.driver = webdriver.Firefox(my_profile)

self.driver.get(login_url)
self.driver.implicitly_wait(10)
my_iframe = self.driver.find_element_by_css_selector("iframe.hiFrame")
#my_iframe = self.driver.find_element_by_css_selector("iframe:nth-of-type(1)")
#my_iframe = self.driver.find_element_by_tag_name("iframe")
self.driver.switch_to_frame(my_iframe)
try:
element = WebDriverWait(self.driver, 30).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='username']")))
user_input = self.driver.find_element_by_css_selector("input[name='username']")
user_input.send_keys(user)
finally:
print('user name input appeared')

结果:此错误是由 WebDriver 导致的:文件“instagram_firefox.py”,第 51 行,setUp 中 element = WebDriverWait(self.driver, 45).until(EC.visibility_of_element_ located((By.CSS_SELECTOR, "input[name='用户名']"))) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/support/wait.py”,第 71 行,直到 引发超时异常(消息)

我尝试验证输入字段的 css 选择器是否正确。在页面上,https://instagram.com/accounts/login/ ,FireFox FireFinder 无法识别我使用的 css 选择器。但如果我打开另一个带有 iframe 源的选项卡,https://instagram.com/accounts/login/ajax/?targetOrigin=https%3A%2F%2Finstagram.com ,然后 Firefinder 会识别我使用的 css 选择器。这是否意味着我需要手动获取 iframe 源的 url,还是应该在 WebDriver 切换到 iframe 时自动完成?

最佳答案

我们应该首先等待 div spinner 元素消失,然后我们可以检索您需要的 iframe:

user = "user"
self.driver.get("https://instagram.com/accounts/login/")
#Wait for spinner to disappear
WebDriverWait(self.driver, 10).until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "div.liSpinnerLayer")))
#Get iframe and switch to it
my_iframe = self.driver.find_element_by_css_selector("iframe.hiFrame")
self.driver.switch_to_frame(my_iframe)
element = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='username']")))
element.send_keys(user)

关于python - Selenium - 如何调试 iframe > 输入字段的超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25861566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com