【问题标题】:What type of wait should I use for sendKeys function for an element ? Python Selenium我应该为元素的 sendKeys 函数使用什么类型的等待?蟒蛇硒
【发布时间】:2020-09-16 08:36:27
【问题描述】:

鉴于我必须使用 sendKeys 为可能加载缓慢或被动画阻碍的元素属性设置值,我应该使用哪种类型的等待元素?

目前我使用 selenium 的 expected_conditions 库中的 visibility_of_element_located,但由于某种原因,有时,输入没有完成,光标在元素上闪烁,好像找到了但没有插入任何值。

我不确定 visibility_of_element_located 是否是正确的用例,你们有什么建议吗?

编辑:这是一个更“选择你的大脑”类型的问题,而我希望学习更好的用例,如果有的话适用于我上面描述的内容。如果需要任何其他信息,我很乐意在这里丰富。

我用 xpath 和 value 调用的方法,作为强制输入:

    def type_js(self, xpath, value, enter=None, priority=None, enable_Logging=True, js=None, classValue=None, forLoopPause=0.1, type_Token=False):

    if isinstance(value, int) == True:
        value = str(value)

    if enable_Logging == True:
        try:
            WebDriverWait(self.browser, 10).until(ec.visibility_of_element_located((By.XPATH, xpath)))
            elemnt = self.browser.find_element_by_xpath(xpath)
            elemnt.clear()

            if type_Token == True:
                elemnt.click()
                from pynput.keyboard import Key, Controller
                for i in list(value):
                    # elemnt.send_keys(i)
                    keyboard = Controller()
                    keyboard.press(str(i))
                    keyboard.release(str(i))
                    time.sleep(1)
            else:
                if js == True:
                    self.browser.execute_script(
                        "arguments[0].setAttribute('value', '{}');".format(value.replace('\n', '')),
                        self.browser.find_element_by_xpath("{}".format(xpath)))
                else:
                    for i in list('{}'.format(value)):
                        elemnt.send_keys(i)
                        time.sleep(forLoopPause)
                    if enter != None:
                        elemnt.send_keys(Keys.ENTER)

            utilities.loggingObj.priority_handler(xpath=xpath, priority=priority, action_type='Type',
                                                     isException=False, browser=self.browser)

        except(NoSuchElementException, StaleElementReferenceException) as e:
            print(
                'exceptie {} pentru xPath : {}\nException Stack Trace : {}'.format(str(e), xpath,
                                                                                   e))
            utilities.loggingObj.priority_handler(xpath=xpath, priority=priority, action_type='Type',
                                                     isException=True,
                                                     exeption='exceptie {} pentru xPath : {}\nException Stack Trace : {}'.format(
                                                         str(e), xpath,
                                                         e), browser=self.browser)
            return False

        except(TimeoutException) as e_timeout:
            print(
                'exceptie {} pentru xPath : {}\nException Stack Trace : Elementul nu a fost vizibil pe pagina in 3 sec de wait (TimeoutException)'.format(
                    str(e_timeout), xpath))
            utilities.loggingObj.priority_handler(xpath=xpath, priority=priority, action_type='Type',
                                                     isException=True, exeption=
                                                     'exceptie {} pentru xPath : {}\nException Stack Trace : Elementul nu a fost vizibil pe pagina in 3 sec de wait (TimeoutException)'.format(
                                                         str(e_timeout), xpath), browser=self.browser)

【问题讨论】:

  • 您可以使用元素来点击。如果没有任何效果,请尝试 java 脚本执行器。使用与代码、日志和屏幕图像相关的更多信息更新问题。
  • 每运行 50 次测试就会出现一次或两次问题,将使用我用于尽快输入的方法进行更新。虽然这不是帖子的目标
  • 我认为您必须在WebDriverWait(self.browser, 10).until(ec.visibility_of_element_located((By.XPATH, xpath))) 使用“可点击”,而不是检查元素的可见性。
  • 是的,这就是我以前使用的,但我认为还有一些我没有读过的其他功能或者一些更聪明的方法来解决这个问题。虽然我确实认为在这种情况下“可点击”更好,但是,谢谢

标签: java python selenium selenium-webdriver


【解决方案1】:

查看等待情况 - 它可能会对您有所帮助 Python Selenium Waits

Selenium Webdriver 提供两种类型的等待 - 隐式和显式。 显式等待使 WebDriver 等待某个条件发生 在继续执行之前。隐式等待使 WebDriver 尝试在一定时间内轮询 DOM 定位一个元素。

【讨论】:

  • 阅读这些伙伴,我想知道我的用例是否不是最佳的。谢谢你!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 2016-05-28
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
  • 2023-02-06
相关资源
最近更新 更多