我编写了一个测试脚本,使用chrome浏览器模拟手机模式。文本输入没问题,但是click事件没有响应。出现错误消息:
60秒后点击超时。
我使用了以下NuGet包:
Selenium.Support 3.8.0
Selenium.WebDriver 3.8.0
Selenium.WebDriver.ChromeDriver 2.34.0
以下是我的代码:
ChromeOptions cos = new ChromeOptions();
string userAgent ="Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1";
//string userAgent = "Linux; Android 4.1.1; GT-N7100 Build/JRO03C) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/35.0.1916.138 Mobile Safari/537.36 T7/6.3";
cos.EnableMobileEmulation(new ChromeMobileEmulationDeviceSettings() { Width = 375, Height = 667, UserAgent = userAgent , EnableTouchEvents=true, PixelRatio=3.0 });
//cos.EnableMobileEmulation(new ChromeMobileEmulationDeviceSettings() { Width = 320, Height = 540, UserAgent = userAgent, EnableTouchEvents = true, PixelRatio = 3.0 });
IWebDriver driver = new ChromeDriver(cos);
driver.Manage().Window.Size = new System.Drawing.Size(395, 810);//
driver.Navigate().GoToUrl("https://m.baidu.com/");
Console.WriteLine("Page Loaded");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
try
{
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("index-kw")));
var item = driver.FindElement(By.Id("index-kw"));
item.SendKeys("selenium c#");
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
Console.WriteLine("text enter");
try
{
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("index-bn")));
var item = driver.FindElement(By.Id("index-bn"));
item.Click();
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
Console.WriteLine("finished");
Console.ReadLine();
答案 0 :(得分:1)
我之前遇到过Click()问题,我认为这是由于按钮点击时执行的任何javascript与Click()尝试执行的操作相冲突。
我已经使用item.SendKeys(Keys.Enter)解决了这些问题。可能值得一试。