简体   繁体   English

使用Selenium WebDriver设置焦点

[英]Setting focus with Selenium WebDriver

I have a Selenium WebDriver test where I enter some text into a text input box 我有一个Selenium WebDriver测试,我在文本输入框中输入了一些文本

var input_Note = Driver.Instance.FindElement(By.Id("note"));
        input_Note.SendKeys("test");

I then attempt to click on the Save button, but it does not work. 然后,我尝试单击“保存”按钮,但是它不起作用。 I was previously using Coded UI where there is a SetFocus element that points the focus towards whichever element you are targeting. 我以前使用的是编码UI,其中有一个SetFocus元素,它将焦点指向您要定位的任何元素。 Is there something similar in Selenium? 硒中有类似的东西吗?

var button_Save = Driver.Instance.FindElement(By.Id("save"));
button_Save.Submit();

Sometimes depending on how the page is loaded it will exist and then not exist and then exist again. 有时,根据页面的加载方式会存在,然后不存在,然后再存在。 I have found that waiting on the element is a good idea and sometimes will put two waits back to back for the specific element that this will solve this issue(I would say a programming fix for a double wait would be desired...screen flashing too much at that point). 我发现在元素上等待是个好主意,有时会为解决该问题的特定元素放回两次等待(我想说需要两次等待的编程修复...屏幕闪烁在那点太多)。 This really depends on the load pattern of your application though. 不过,这实际上取决于应用程序的加载模式。

    WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
    wait.Until (d=> Driver.Instance.FindElement(By.Id("save")));

I would also utilize the .click if it is a button. 如果它是一个按钮,我也会利用.click。 In general it should be a submit action, but it doesn't always have to be a submit action. 通常,它应该是一个提交动作,但并不总是必须是一个提交动作。 There may also be situations where you might need to gain the focus...which shouldn't be programmed that way, but in case it is you can utilize the Actions class and move the mouse to the element and then perform a click action on the element. 在某些情况下,您可能需要获取焦点...不应以这种方式进行编程,但是如果是这种情况,则可以利用Actions类并将鼠标移到元素上,然后对元素。

    //C# example:
    OpenQA.Selenium.UI.Interactions.Actions actions = new OpenQA.Selenium.UI.Interactions.Actions();
    actions.MoveToElement([Instance of Web Element goes here]).Perform();
    actions.Click([Instance of Web Element goes here]).Perform();

In general you could just use the actions.Click, but figured I would give both. 一般来说,你可以只使用actions.Click,但想我会放弃两者。

One of the above should work just fine. 以上之一应该可以正常工作。 If it does not work please provide a specific error message you get with Selenium and the specific html structure of the page being utilized. 如果它不起作用,请提供您收到的与Selenium相关的特定错误消息以及所使用页面的特定html结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM