C# 使用selenium C webdriver从标记类型-输入下拉列表中选择
如何从下面的DropdOn中获取元素,我使用了Select命令来选择下拉列表,但这里的类型是输入C# 使用selenium C webdriver从标记类型-输入下拉列表中选择,c#,selenium-webdriver,C#,Selenium Webdriver,如何从下面的DropdOn中获取元素,我使用了Select命令来选择下拉列表,但这里的类型是输入 <input class="tp-select-input" autocapitalize="none" autocorrect="off" autocomplete="off" spellcheck="false" data-testid="register-country"
<input class="tp-select-input" autocapitalize="none" autocorrect="off" autocomplete="off" spellcheck="false" data-testid="register-country" value="">
由于要从中选择值的下拉列表不是选择类型标记,Selenium select方法无法对其进行操作。您必须遵循以下步骤: 单击下拉列表/或箭头 找到要从下拉列表中选择的webelement 使用java脚本执行器单击要选择的选项 由于您尚未给出完整的HTML,下面是伪代码:
driver.FindElement(By.XPath("//input[@data-testid='register-country']")).Click() // Click on dropdown
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[text()='Austria']"))); //You can give more accurate xpath for country you want to select based on other HTML attributes
您也可以尝试使用以下代码
IWebElement元素=driver.FindElementBy.ClassNametp-select-input;driver.FindElementBy.XPath//span[text='Australia'];element.SendKeysOpenQA.Selenium.Keys.Down;element.SendKeysOpenQA.Selenium.Keys.Return 您是否尝试通过CSS/XPath将其定位为公共WebElement?需要有关HTML的更多信息。是否有任何箭头符号单击,您的下拉选项可见?从下拉列表中选择哪些选项?我已经尝试过:SelectElement oSelect=new SelectElementdriver.FindElementBy.ClassNametp-select-inputMate我无法帮助您处理图像,您需要共享HTML源代码。然而,我在回答部分给出了逻辑。请实施它。@KishanRathod-为什么你在一开始接受了我的答案之后就不接受我的答案??它没有解决你的问题吗???对不起,拉胡尔,我没有拒绝你的答案,但可能是我也接受了其他答案,你的答案自动拒绝,我再次接受。