用Try和Except语句切换屏幕PythonKivySelenium



我正在为一个需要在页面中唱歌的web scraper(Selenium和Kivy(制作GUI。有时它会说要验证你的帐户,理论上,如果硒驱动程序找到了该页面上特定的元素,它应该切换屏幕(Try和Except(。如果它找不到元素,它应该添加一条错误消息。然而,这并没有切换屏幕,而是停留在第一个屏幕上。我已经对此进行了一段时间的故障排除,我不确定问题出在哪里。当Selenium驱动程序找到元素时,它会运行try语句中的代码。如果没有,则不会添加错误消息。

这是我的python代码:

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.config import Config
import linkedenInfo
from linkedenInfo import *
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
Config.set('graphics', 'resizable', True)
Fl = FloatLayout()
#---
class SignInScreen(Screen):
email = ObjectProperty(None)
pwd = ObjectProperty(None)
def sign_in(self):
user = UsDefined()
errMsg = Label(text="Incorrect Username or Passoword!nPlease Try again", size_hint=(.5, 0.05),
pos_hint={'top': 0.6, 'center_x': 0.5})
try:
user.sign_in(self.email.text, self.pwd.text)
driver.find_element_by_xpath('//*[@id="global-nav"]')
except NoSuchElementException:
try:
driver.find_element_by_xpath('//*[@id="app__container"]/main/h1')
LinkedInGui.screen_switch_one(None)
except NoSuchElementException:
self.email.text = ""
self.pwd.text = ""
Fl.add_widget(errMsg)
#----

class VerifScreen(Screen):
verifInput = ObjectProperty(None)
class ScreenMang(ScreenManager):
pass
kv = Builder.load_file("appFormatting.kv")
sm = ScreenManager()
sm.add_widget(VerifScreen(name="_VerifScreen_"))
class LinkedInGui(App):
def build(self):
return kv
def screen_switch_one(self):
print("Hello 2!")
sm.current_screen = "_VerifScreen_"

# run the App

if __name__ == "__main__":
LinkedInGui().run()

这是我的KV文件:


ScreenMang:
SignInScreen:
VerifScreen:

<SignInScreen>
name: "Sign-In"
email: userEmail
pwd: password
FloatLayout:
Label:
text:"LinkedIn Web-Scraper"
size_hint:(.5, 0.05)
pos_hint:{'top': 0.975, 'center_x': 0.5}
TextInput:
id: userEmail
hint_text:"LinkedIn Email: "
size_hint:(.5, 0.05)
pos_hint:{'center_x': 0.5, 'y': .85}
multiline:False
TextInput:
id: password
hint_text:"LinkedIn Password: "
size_hint:(.5, 0.05)
pos_hint:{'center_x': 0.5, 'y': .8}
multiline:False
password:True
Button:
text:"Sign-In"
size_hint:(.5, 0.1)
pos_hint:{'top': 0.76, 'center_x': .5}
on_release:
root.sign_in()

<VerifScreen>
name: "_VerifScreen_"
verifInput: verifInput
FloatLayout:
Label:
text: "An SMS text message was sent to your phone number.nEnter the code below."
size_hint: (.5, 0.05)
pos_hint: {'top': 0.79, 'center_x': 0.5}
TextInput:
id: verifInput
hint_text: "Verification Code"
size_hint: (.5, 0.05)
pos_hint: {'top': 0.73, 'center_x': 0.5}
Button:
text:"Verify"
size_hint:(.5, 0.1)
pos_hint:{'top': 0.6, 'center_x': .5}
#            on_release:
#                root.sign_in()

这是我的Selenium代码:

class UsDefined():
def sign_in(self, usname, pwd):
url = 'https://www.linkedin.com/uas/login?session_redirect=https%3A%2F%2Fwww%' 
'2Elinkedin%2Ecom%2Fsearch%2Fresults%2Fpeople%2F%3Forigin%3DDISCOVER_FROM_' 
'SEARCH_HOME&fromSignIn=true&trk=cold_join_sign_in'
driver.get(url)
driver.find_element_by_id("username").clear()
driver.find_element_by_id("username").send_keys(usname)
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys(pwd) 
driver.find_element_by_id("password").send_keys(Keys.RETURN)
def verif_code(self, verifNum):
verifText = driver.find_element_by_xpath('//*[@id="app__container"]/main/h1').text
driver.find_element_by_xpath('//*[@id="input__phone_verification_pin"]').send_keys(verifNum)
driver.find_element_by_xpath('//*[@id="input__phone_verification_pin"]').send_keys(Keys.RETURN)

为什么我不能通过python代码切换屏幕?我会在KV文件中编写try-and-except语句,但这是不可能的。

您是否尝试过在没有"NoSuchElementException"?

只是一个";除了:";

try:
user.sign_in(self.email.text, self.pwd.text)
driver.find_element_by_xpath('//*[@id="global-nav"]')
except:
try:
driver.find_element_by_xpath('//*[@id="app__container"]/main/h1')
LinkedInGui.screen_switch_one(None)
except:
self.email.text = ""
self.pwd.text = ""
Fl.add_widget(errMsg)

这不是最好的方法,但您可以隔离错误。

相关内容

  • 没有找到相关文章

最新更新