Selenium:Selenium中的summary_of_element_located()聚合

时间:2019-01-02 02:46:22

标签: python selenium

我正在使用Python尝试Selenium。有没有办法进一步简化代码?

CLASS_NAME = "CLASS_NAME"
CSS_SELECTOR = "CSS_SELECTOR"
ID = "ID"
def check_element_type(element_type, element_string):
    if element_type == CLASS_NAME:
        return WebDriverWait(browser, timeout).until(EC.presence_of_element_located((By.CLASS_NAME, element_string)))
    elif element_type == CSS_SELECTOR:
        return WebDriverWait(browser, timeout).until(EC.presence_of_element_located((By.CSS_SELECTOR, element_string)))
    elif element_type == ID:
        return WebDriverWait(browser, timeout).until(EC.presence_of_element_located((By.ID, element_string)))  

这是概念目标,但无效

def get_element(element_type, element_string):  
    return WebDriverWait(browser, timeout).until(EC.presence_of_element_located((By.element_type, element_string)))  

如果我在具有相同名称selenium.webdriver.common.byCLASS_NAME = "CLASS_NAME"的常量字符串的情况下显式导入CSS_SELECTOR = "CSS_SELECTOR",则会引发错误。

1 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <your_package.CustomView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#f00" app:image="@drawable/ic_android" app:text="Android" /> <your_package.CustomView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#0f0" app:image="@drawable/ic_adb" app:text="ADB" /> </LinearLayout> 返回字符串

By

在您的方法中,将class By(object): """ Set of supported locator strategies. """ ID = "id" XPATH = "xpath" LINK_TEXT = "link text" PARTIAL_LINK_TEXT = "partial link text" NAME = "name" TAG_NAME = "tag name" CLASS_NAME = "class name" CSS_SELECTOR = "css selector" 更改为By.element_type

element_type

并像使用它

def get_element(element_type, element_string):  
    return WebDriverWait(browser, timeout).until(EC.presence_of_element_located((element_type, element_string)))

element = get_element("css selector", "div.myClass")
element = get_element("class name", "myClass")
element = get_element("xpath", "//div[@='myClass']")