硒页面源缺少元素 [英] Selenium Page Source is Missing Elements
问题描述
我有一个基本的Selenium脚本,它使用了chromedriver二进制文件。我正在尝试显示一个上面带有Recaptcha的页面,然后挂起,直到答案完成,然后将其存储在变量中以备将来使用。
I have a basic Selenium script that makes use of the chromedriver binary. I'm trying to display a page with recaptcha on it and then hang until the answer has been completed and then store that in a variable for future use.
我遇到的障碍是我找不到Recaptcha元素。
The roadblock I'm hitting is that I am unable to find the recaptcha element.
#!/bin/env python2.7
import os
from selenium import webdriver
driverBin=os.path.expanduser("~/Desktop/chromedriver")
driver=webdriver.Chrome(driverBin)
driver.implicitly_wait(5)
driver.get('http://patrickhlauke.github.io/recaptcha/')
可以看到此元素吗?
还有没有办法在用户解决后获取令牌而不刷新页面?
Also is there a way to grab the token after user solve without refreshing the page?
现在,隐藏密码令牌ID的输入类型被隐藏了。解决后,将创建第二个Recaptcha令牌ID。这是我希望存储在变量中的值。我当时正在考虑使用该ID循环检查找到的元素的长度。如果大于1,则解析。但我不确定来源本身是否会更新。
As it is now the input type of the recaptcha-token id is hidden. After solve a second recaptcha-token id is created. This is the value I wish to store in a variable. I was thinking of having a loop of checking length of found elements with that id. If greater than 1 parse. But I'm unsure whether the source updates per se.
更新:
随着更多的研究,它与元素的性质有关,尤其是:与标记:<输入类型= hidden
。因此,我想改写我的问题,一个人如何提取隐藏元素的值。
With more research it has to do with the nature of the element, particularly: with the tag: <input type="hidden"
. So I guess to rephrase my question, how does one extract the value of a hidden element.
推荐答案
您正在寻找的元素(输入
)在iframe中。在定位元素并与之交互之前,您需要切换到iframe。
The element you are looking for (the input
) is in an iframe. You'll need switch to the iframe before you can locate the element and interact with it.
import os
from selenium import webdriver
driver=webdriver.Chrome()
try:
driver.implicitly_wait(5)
driver.get('http://patrickhlauke.github.io/recaptcha/')
# Find the iframe and switch to it
iframe_path = '//iframe[@title="recaptcha widget"]'
iframe = driver.find_element_by_xpath(iframe_path)
driver.switch_to.frame(iframe)
# Find the input element
input_elem = driver.find_element_by_id("recaptcha-token")
print("Found the input element: ", input_elem)
finally:
driver.quit()
这篇关于硒页面源缺少元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!