我想自动登录到浏览器游戏(https://kingdoms.com/com),使一些自动升级。但我正在努力登录屏幕。步骤非常简单(如图所示)
1-点击登录按钮。
2-新的窗口正在打开,我认为这是一个新的框架。
3-查找框架类以便将主框架切换到新框架。
我也尝试过直接填充邮箱而不切换帧。
点击这里查看图片-登录页面
我的代码在这里:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://kingdoms.com/com")
driver.implicitly_wait(5)
loginButton = driver.find_element_by_id("loginButton").click()
driver.implicitly_wait(5)
frame_login = driver.find_element_by_class_name("mellon-iframe")
driver.switch_to.frame(frame_login)
driver.implicitly_wait(2)
driver.find_element_by_name("email").send_keys("@gmail.com")
看到我的错误:
Traceback (most recent call last):
File "C:Usersonder.lapcinPycharmProjectsProjectTestSelenium_projectstravian_giris.py", line 17, in <module>
driver.find_element_by_name("email").send_keys("@gmail.com")
File "C:Usersonder.lapcinAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesseleniumwebdriverremotewebdriver.py", line 496, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:Usersonder.lapcinAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesseleniumwebdriverremotewebdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:Usersonder.lapcinAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:Usersonder.lapcinAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesseleniumwebdriverremoteerrorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="email"]"}
(Session info: chrome=94.0.4606.81)
我明白了我的错误。有两个框架。我要从主帧切换到第二帧。但我无法看到第三帧到达邮箱和密码框。
frame_login = driver.find_element_by_xpath("//iframe[@class='mellon-iframe']")
driver.switch_to.frame(frame_login)
frame_login = driver.find_element_by_xpath("//iframe")
driver.switch_to.frame(frame_login)
现在可以了