作者A601B8 (MBD)
看板Python
标题[问题] 自订function该输入哪些变数?
时间Sun Jun 2 00:25:47 2019
最近在学习selenium webdriver寻找元素时
常需要重复打
find_element_by_id或xpath或name等等....
如是还要加上explicit wait时
就会让code变得又臭又常
刚好在逛论坛时看到有个网友PO了一个自订function
======
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from abc import abstractmethod
class LocatorMode:
XPATH = "xpath"
CSS_SELECTOR = "cssSelector"
NAME = "name"
ID = "id"
TAG_NAME = "tagName"
class BasePage(object):
def __init__(self, driver):
self.driver = driver
def wait_for_element_visibility(self, waitTime, locatorMode, Locator):
element = None
if locatorMode == LocatorMode.ID:
element = WebDriverWait(self.driver, waitTime).\
until(EC.visibility_of_element_located((By.ID, Locator)))
elif locatorMode == LocatorMode.NAME:
element = WebDriverWait(self.driver, waitTime).\
until(EC.visibility_of_element_located((By.NAME, Locator)))
elif locatorMode == LocatorMode.XPATH:
element = WebDriverWait(self.driver, waitTime).\
until(EC.visibility_of_element_located((By.XPATH, Locator)))
elif locatorMode == LocatorMode.CSS_SELECTOR:
element = WebDriverWait(self.driver, waitTime).\
until(EC.visibility_of_element_located((By.CSS_SELECTOR,
Locator)))
else:
raise Exception("Unsupported locator strategy.")
return element
=====
我试了好多次都没办法成功呼叫这个function
我现在希望的状况是
1.用tagname来找element,且假设网页中element的tagname="tr"
2.EC是在visibility_of_element_located的状况
3.等待时间是10秒
所以我自己试了一下
element = BasePage.wait_for_element_visibility(10,tagName,"tr")
element.click()
但如果用这样的input执行的话
系统会返回:
name 'tagName' is not defined
要是换成
element = BasePage.wait_for_element_visibility(10,"tagName","tr")
element.click()
系统会返回:
wait_for_element_visibility() missing 1 required positional argument:
'Locator'
请问我要怎麽输入括号内的变数,才能成功执行BasePage里面的function呢?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 111.242.142.44
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1559406349.A.DFF.html
1F:推 CaptainH: 你跳关了,先把语言本身的语法基础弄懂再来搞爬虫之类 06/02 02:58
2F:→ CaptainH: 的 06/02 02:58
3F:推 jiyu520: 基本观念走一次 06/02 07:28
4F:推 art1: 似乎是要多用一层小括号把你的 3 个引数包起来 06/02 07:32
5F:推 art1: 又好像是呼叫 BasePage 时需要先加一个 driver 的引数? 06/02 08:11
6F:推 sean50301: 你的BasePage没有建出instance 所以少传一个self 06/02 10:49
7F:→ vi000246: 同一楼 你要先把变数、类别这章再读过一遍 06/02 10:51
8F:→ vi000246: 看完後你就会发现 要传进去的是LocatorMode里的member 06/02 10:52
9F:→ vi000246: 然後你可能会遇到sean大提到的问题 把类别这章读熟吧 06/02 10:56
10F:推 art1: 这种少了第一个参数,但错误讯息却是说你少了最後一个参数 06/02 15:05
11F:→ art1: 对类别不熟的人就会被误导了 06/02 15:06