使用selenium调用我的函数时,Python'str'对象不是可调用的消息

2024-04-16 16:50:34 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我尝试实现简单的login到网站:

驱动程序

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait


class Driver(object):
    driver = webdriver.Chrome(executable_path="C:\\Program Files\\Google\\Drive\\chromedriver.exe")
    WebDriverWait30Seconds = WebDriverWait(driver, 30)
    WebDriverWait60Seconds = WebDriverWait(driver, 60)
    WebDriverWait120Seconds = WebDriverWait(driver, 120)

    def close(self):
        """
        close the webdriver instance
        """
        self.driver.quit()

    def navigate(self, url):
        """
        navigate webdriver to different pages
        """
        self.driver.maximize_window()
        self.driver.get(url)

登录页面

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as expected_conditions

from Driver import Driver


class LoginPage(object):

    def __init__(self):
        self.user_name_textbox = Driver.WebDriverWait120Seconds.until(
            expected_conditions.visibility_of(By.ID("login_email")))

    def get_user_name_textbox(self):
        return self.user_name_textbox

Main

from Driver import Driver
from LoginPage import LoginPage

driver = Driver()
driver.navigate("http://blabla.co.il")

logingage = LoginPage()
logingage.user_name_textbox.send_keys("test")

出现了这个错误:

> Traceback (most recent call last):   File
> "C:/Users/user/PycharmProjects/myscript/Main.py", line 7, in
> <module>
>     logingage = LoginPage()   File "C:\Users\user\PycharmProjects\myscript\LoginPage.py", line 10, in
> __init__
>     self.user_name_textbox = expected_conditions.visibility_of_all_elements_located(By.ID("login_email"))
> TypeError: 'str' object is not callable

Tags: namefromimportselfdefdriverseleniumconditions
1条回答
网友
1楼 · 发布于 2024-04-16 16:50:34

By.IDstr(简称'id')而不是可调用函数。你知道吗

在您的例子中,visibility_of_all_elements_located请求一个类似元组的定位器

self.user_name_textbox = Driver.WebDriverWait120Seconds.until(
    expected_conditions.visibility_of(driver.find_element_by_id('login_email')))

相关问题 更多 >