Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium WebDriver–;检查元素的方法的操作不一致_Selenium_Webdriver - Fatal编程技术网

Selenium WebDriver–;检查元素的方法的操作不一致

Selenium WebDriver–;检查元素的方法的操作不一致,selenium,webdriver,Selenium,Webdriver,我有一个网页,它使用DatePicker控件输入DoB作为搜索条件之一。我必须解决的问题是datepicker隐藏了执行搜索的按钮。为了摆脱日期选择器,我单击另一个字段,然后使用一种方法,该方法应该等到搜索按钮可见后再单击它。我的问题是,有时这是可行的,但更多的时候它没有,我得到的元素是不可点击的。。其他元素将接收单击异常。呼叫代码如下 WebDriverSupport.ClearAndEnterDataIntoFieldById(driver, "txtDateOfBirthNew", "01

我有一个网页,它使用DatePicker控件输入DoB作为搜索条件之一。我必须解决的问题是datepicker隐藏了执行搜索的按钮。为了摆脱日期选择器,我单击另一个字段,然后使用一种方法,该方法应该等到搜索按钮可见后再单击它。我的问题是,有时这是可行的,但更多的时候它没有,我得到的元素是不可点击的。。其他元素将接收单击异常。呼叫代码如下

WebDriverSupport.ClearAndEnterDataIntoFieldById(driver, "txtDateOfBirthNew", "01/01/1980"); // causes the datepicker to appear
WebDriverSupport.ClickElementById(driver, "txtSurnameNew");   // to close date picker click on another field
WebDriverSupport.ClickElementById(driver, "btnSearch"); // wait for the search button to appear and click
ClickElementById方法定义为

/// <summary>
/// WebDriver: Clicks on an element
/// </summary>
/// <param name="driver">WebDriver object</param>
/// <param name="id">Name of element (by ID) to click</param>
/// <returns>None</returns>
public static void ClickElementById(IWebDriver driver, string id)
{
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id(id)));
    element.Click();
}
//
///WebDriver:单击元素
/// 
///WebDriver对象
///要单击的元素名称(按ID)
///没有
公共静态无效ClickElementById(IWebDriver驱动程序,字符串id)
{
WebDriverWait wait=新的WebDriverWait(驱动程序,TimeSpan.FromSeconds(10));
IWebElement=wait.Until(ExpectedConditions.ElementIsVisible(By.Id(Id));
元素。单击();
}
如果我在调试模式下运行,并在单击搜索按钮的语句上放置一个断点,然后从那里继续,那么每次都可以工作。但我想这是因为有时间让日期选择器消失,按钮变得可见。 我无法理解的是ClickElementById方法正在等待元素在执行单击之前可见,那么为什么会出现异常呢?以及为什么它在某些时候有效而在其他时候无效


对解决这个问题有什么建议吗?是否有更可靠的方法来检查元素是否可见并可单击?

ElementIsVisible
检查是否至少显示了部分元素。但是,对于
click()
方法来说,它并不总是足够好。尝试从类中使用
MoveToElement


不幸的是,这不起作用。对我来说,它看起来像是ExpectedConditions.ElementIsVisible并不是一个可靠的测试,因为元素是可见和活动的,所以可以“单击”它。它们是我们可以使用的更好的测试吗?@Cooldudescrib您可以尝试
ElementToBeClickable()
等待.Until(ExpectedConditions.ElementToBeClickable(By.Id(Id))谢谢这看起来是可行的,但效果不一致。我运行我的代码,它工作。然后,我在调试模式(没有断点)下运行代码,但它不起作用-我发现元素不可单击。。其他元素将接收单击异常。正是这种不一致性让我感到沮丧,因为有些东西在某一时间起作用,而在另一时间却不起作用。这种情况下,很难建立一套可靠的测试。
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id(id)));

Actions actions = new Actions(driver);
actions.MoveToElement(element).Perform();

element.Click();