http://wuxia.qq.com/cp/a20160920tmfl/ 天涯明月刀的网站,想自动领取奖励,登陆这块就遇到问题
打开网站后,点击登陆,切换到 loginIframe 登陆 frame 输入用户名密码正确,但是登陆后无法控制验证码所在的 frame ,看不出名字,没法切换也就无法控制
代码如下:
tyurl = "http://wuxia.qq.com/cp/a20160920tmfl/"
username = "123456"
pwd = "1234561"
binary = FirefoxBinary("z:\\firefox\\firefox.exe")
driver = webdriver.Firefox(firefox_binary=binary,executable_path = "z:\geckodrive\geckodriver.exe")
driver.get(tyurl)
driver.find_element_by_id("dologin").click()
driver.switch_to_frame("loginIframe")
# 等待账号密码登录窗口出现,点击账号密码登
WebDriverWait(driver, 10,1).until(EC.presence_of_element_located((By.ID,"switcher_plogin")))
driver.find_element_by_id("switcher_plogin").click()
# 输入账号密码
WebDriverWait(driver, 30,1).until(EC.presence_of_element_located((By.ID,"login_button")))
for i in range(1,20):
try:
driver.find_element_by_id("u").click()
driver.find_element_by_id("u").send_keys(username)
driver.find_element_by_id("p").click()
driver.find_element_by_id("p").send_keys(pwd)
break
except Exception, e:
pass
# 输入密码点击确定后无法定位验证码窗口
try:
WebDriverWait(driver, 5,1).until(EC.presence_of_element_located((By.ID,"capImg")))
except Exception, e:
pass
# 找不到当前验证码窗口里的 ID capImg
print(driver.find_element_by_id("capImg").src)
1
xrlin 2016-10-24 23:42:28 +08:00
试一下使用 driver.switch_to_default_content(), 我当初是这么解决类似问题的。
|
2
bbxiong OP driver.switch_to_default_content() 试验过了,不行,也是报错.
|
3
SlipStupig 2016-10-25 00:22:27 +08:00
driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[title='Fill Quote']")));
|
4
bbxiong OP driver.switch_to_frame(driver.find_element_by_css_selector("iframe[title='Fill Quote']"));
这个也不行: selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: iframe[title='Fill Quote'] |
5
kittyyc713 2016-10-26 10:05:01 +08:00
我比较好奇你怎么破验证码。。人工吗
|
6
bbxiong OP 正确方法, switch_to_frame 支持对象参数,传递当前页面的 iframe 就可以额
driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0]) 记得还要调出来 driver.switch_to.default_content() |
7
bbxiong OP 验证码用各种答题服务器平台就可以。
|