元素在Python中不可交互

2024-05-29 03:16:39 发布

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

我从Python-Selenium开始,无法与密码部分交互,也无法从文本文件中插入密码。我认为问题不在于插入或定位元素,因为用户名定位和插入是正确的。问题可能出在等待中,但我无法在代码中正确表达此命令。插入用户名后,页面被滑动到下一页,应该有一些延迟。所以我尝试了函数“time.sleep(4)”,但它不起作用,但即使是函数“driver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS)”也不起作用。我尝试了许多制定这个命令的选项,但代码总是以Message:element not interactiable结束。你能帮我解决这个问题吗? 下面是代码中有问题的部分:

driver.find_element_by_id("userName").send_keys(username2)
driver.find_element_by_id("verify_user_btn").click()

time.sleep(4)
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS)
driver.find_element_by_id("password").send_keys(password2)
driver.find_element_by_id("btnSubmit").click()

非常感谢


Tags: 函数代码定位命令id密码bymanage
2条回答

使用显式等待而不是隐式等待。要求selenium等待元素密码可见。此外,在单击按钮之前,请检查按钮是否显示并已启用

//密码 wait.Until(driver => driver.FindElement(By.ID("password")).Displayed;

//用于按钮 wait.Until(driver => driver.FindElement(element).Enabled && driver.FindElement(element).Displayed;

注:这些是C代码

下面是一些python方法,用于等待其中一个元素在DOM中显示、可单击或显示。只需传入诸如ID、Name等类型和定位器。例如,pass-in By.ID和“password”

def wait_for_element_presence(self, by_type, locator, wait_seconds=10):
    self.log.debug("Waiting for presence of element located")
    result_flag = False
    try:
        WebDriverWait(self.driver, wait_seconds).until(ec.presence_of_element_located((
            by_type, locator)))
        result_flag = True
    except Exception as e:
        self.log.warning("An exception occurred, see below for details: ")
        self.log.error(e, exc_info=True)
    return result_flag


def wait_for_element_to_be_displayed(self, by_type, locator, wait_seconds=10):
    self.log.debug("Waiting for element to be displayed")
    result_flag = False
    try:
        WebDriverWait(self.driver, wait_seconds).until(
            lambda x: x.find_element(by_type, locator).is_displayed())
        result_flag = True
    except TimeoutException:
        self.log.error("User supplied element locator: by {0} '{1}'".format(by_type, locator))
    return result_flag

def wait_for_element_to_be_clickable(self, by_type, locator, wait_seconds=10):
    self.log.debug("Waiting for element to be clickable")
    result_flag = False
    try:
        WebDriverWait(self.driver, wait_seconds).until(ec.element_to_be_clickable((
            by_type, locator)))
        result_flag = True
    except Exception as e:
        self.log.warning("An exception occurred, see below for details: ")
        self.log.error(e, exc_info=True)
    return result_flag

相关问题 更多 >

    热门问题