【问题标题】:Check If element exists in python selenium检查python selenium中是否存在元素
【发布时间】:2018-07-24 07:19:40
【问题描述】:

以下是我的 python selenium 代码

def provideDetails(self, titleEleName, titleText, sbmtBtnName, dropdownOptn, dropdownName):

        self.ui.jobs.setJobTitleEleName(titleEleName, titleText)

        elem = self.ui.jobs.selectButton(dropdownName)

        if elem.is_displayed():
            self.ui.jobs.selectButton(dropdownName)
            self.ui.sleep(4)
            self.ui.jobs.selectAor(dropdownOptn)
        else:
            self.ui.jobs.selectAddAors(dropdownOptn)
            self.ui.sleep(4)

        self.ui.jobs.selectButton(sbmtBtnName)

我必须检查'elem'是否存在。 如果它存在,'if' 条件应该发生,如果不存在,'else' 条件应该起作用。 我试过这段代码。我得到了这个错误“属性错误:无类型对象没有属性'is_displayed'。 任何帮助,将不胜感激。谢谢。

另外,是否有任何替代方法来检查元素是否存在并遵循 if else 命令

【问题讨论】:

  • 你能在进入循环之前检查一下elem 持有什么吗?
  • @eduPeeth elem 点击按钮。
  • 在selectButton方法中提供代码
  • @muraliselenium def _selectSpan(self, span): """按文本点击 span - 提交、取消等""" self.ui.driver.find_element_by_xpath("//span[contains (text(),\'{}\')]".format(span)).click() def selectButton(self, button): """在提供工作、船员等的详细信息页面中单击一个按钮。 """ self._selectSpan(button)

标签: python-3.x selenium if-statement xpath ui-automation


【解决方案1】:

一个好方法是使用explicit wait。检查元素是否存在于页面的 DOM 上并且可见的期望。可见性是指元素不仅被显示出来,而且高度和宽度都大于0。看例子:

self.ui.jobs.setJobTitleEleName(titleEleName, titleText)
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 5)

try:
    wait.until(EC.visibility_of_element_located((By.NAME, dropdownName)))
    self.ui.jobs.selectButton(dropdownName)
    self.ui.sleep(4)
    self.ui.jobs.selectAor(dropdownOptn)
except TimeoutException:
    self.ui.jobs.selectAddAors(dropdownOptn)
    self.ui.sleep(4)

self.ui.jobs.selectButton(sbmtBtnName)

if / else 语句的解决方案:

def provideDetails(self, titleEleName, titleText, sbmtBtnName, dropdownOptn, dropdownName):
    self.ui.jobs.setJobTitleEleName(titleEleName, titleText)
    browser = webdriver.Chrome()
    wait = WebDriverWait(browser, 5)
    elem = wait.until(EC.visibility_of_any_elements_located((By.NAME, dropdownName))) # will return a list of elements

    if elem:
        self.ui.jobs.selectButton(dropdownName)
        self.ui.sleep(4)
        self.ui.jobs.selectAor(dropdownOptn)
    else:
        self.ui.jobs.selectAddAors(dropdownOptn)
        self.ui.sleep(4)

    self.ui.jobs.selectButton(sbmtBtnName)

检查网页上是否至少有一个可见元素的期望。定位器用于查找元素,一旦定位,返回 WebElements 列表。

进口:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

【讨论】:

  • 如果找到元素并不意味着它被显示。不要误导OP。您可以将代码更新为if elem and elem[0].is_displayed():
  • @Andersson 对不起。我已根据最佳实践重写了我的答案。请检查一下,我对您的意见很感兴趣。
  • 好的。但是现在看来该解决方案不符合使用if/else构造的OP要求
  • 我明白了。让我们考虑两个解决方案。
【解决方案2】:

根据评论selectButton 将执行点击操作但不返回网页元素。让我用java解释一下

WebElement e = driver.findElement(By.xpath("//elementPath"));

这里是e 是我可以点击的网页元素

 e.click();

或者可以验证元素是否显示(在if条件下)

e.isDisplayed(); 

但是WebElement e = driver.findElement(By.xpath("//elementPath")).click(); 是无效的。在 Java 中,一旦您编写它,它就会在 eclipse 等编辑器中向您显示异常。所以driver.findElement(By.xpath("//elementPath")).click(); 不会返回元素来检查是否显示。

【讨论】:

    猜你喜欢
    • 2018-01-23
    • 2021-05-17
    • 1970-01-01
    • 2012-03-22
    • 2016-08-05
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    相关资源
    最近更新 更多