Home > other >  Def sendKey, def elementclick call def getElement to positioning element type failure, how to change
Def sendKey, def elementclick call def getElement to positioning element type failure, how to change

Time:09-19

# def sendKey, def elementclick call def getElement failed to positioning element type judgment,
Vision: click element is XPTH type, called def getElement, if not loactorType "id", to identify can enter getByType, and successfully read related elements positioning information
Results: call def getElement, if not loactorType "id", as if did not enter to identify getByType, result in error reminder "selenium.com mon. Exceptions. NoSuchElementException: Message: no to element: Unable to locate the element: {" method" : "xpath", "selector" : "//* [@ id=" db - global - nav "]/div/div [1]/ul/li [2]/a/span [1] "} "


The original code BasePage. Py
The from selenium.webdriver.com mon. By the import by
The from traceback import print_stack

The class BasePage () :
Def __init__ (self, driver) :
The self. The driver=driver

Def getByType (self, loactorType) :
# unified lowercase
LoactorType=loactorType. The lower ()
If loactorType=="id" :
Return By. ID
Elif loactorType=="name" :
Return By the NAME
Elif loactorType=="xpath" :
Return By the XPATH
Elif loactorType=="class" :
Return By. CLASS_NAME
Elif loactorType=="CSS" :
Return By. CSS_SELECTOR
Elif loactorType=="linktext" :
Return By. LINK_TEXT
The else:
Print (" element type: "+ loactorType +" does not exist ")
Return False

Def getElement (self, locator, locatorType="id") :
Element=None
Try:
LocatorType=locatorType. The lower ()
ByType=self. GetByType (locatorType)
Element=self. Driver. Find_element (byType a locator)
Print (" find succss element ")

Except:
Print (" element for failure ")
The return element

Def sendKey (self, data, locator, locatorType="id") :
Try:
# byType=self. GetByType (locatorType)
Element=self. GetElement (locator, locatorType)

element. Send_keys (data)Print (" data sent successfully ")

Except:
Print (" data sent failure ")
Print_stack ()

Def elementclick (self, locator, locatorType="id") :
Try:
# byType=self. GetByType (locatorType)
Element=self. GetElement (locator, locatorType)

element. The click ()Print (" click succss element ")

Except:
Print (" elements "+ locator, click on the" failure ")
Print_stack ()


Related to the original code: test_login. Py
The from the selenium import webdriver
The from selenium.webdriver.com mon. By the import by
The from page. Login_page import LoginPage
The import unittest
The import time

The class LoginTest (unittest. TestCase) :
Def test_login (self) :
URL='https://www.douban.com/'
Driver=webdriver. Chrome ()
Driver. Maximize_window ()
Driver. Implicitly_wait (20)
Driver. The get (URL)

# enter the inside of the iframe box
Driver. Switch_to. Frame (driver. Find_element (By XPATH, '//* [@ id="anony - reg - new"]/div/div [1]/iframe'))
# switch to "account password"
Qf=driver. Find_element (By XPATH, '/HTML/body/div [1]/div [1]/ul [1]/li [2]')
Qf. Click ()
Lp=LoginPage (driver)
Lp. Login (' XXXXX ', '123456')

Driver. Switch_to. Default_content ()

The username=driver. Find_element (By XPATH, '//* [@ id="db - global - nav"]/div/div [1]/ul/li/span [1] [2]/a')
If the username is not None:
Print (" login successful ")
The else:
Print (' login failed ')
Driver. The quit ()



Related to the original code: login_page. Py
The from selenium.webdriver.com mon. By the import by
The from page. BasePage import BasePage

The class LoginPage (BasePage) :
Def __init__ (self, driver) :
Super () __init__ (driver)
The self. The driver=driver

# positioning element
# _iframebox='//* [@ id="anony - reg - new"]/div/div [1]/iframe'
# _loginqf='//* [@ id="anony - reg - new"]/div/div [1]/iframe'
_user_name='username'
_password_pd='password'
_login_bt='/HTML/body/div [1]/div [2]/div [1]/div [5]/a'

Def enternumber (self, number) :
Self. SendKey (number, the self. _user_name)
Def enterpasswd (self, passwd) :
Self. SendKey (passwd, self _password_pd)
Def clickbt (self) :
Self. Elementclick (self. _login_bt)

Def login (self, number, passwd) :
Self. Enternumber (number)
Self. Enterpasswd (passwd)
Self. Clickbt ()

CodePudding user response:

Novice a, inquire
  • Related