using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Support.UI;
//添加缩放属性 将浏览器缩放设为100%
EdgeOptions options=new EdgeOptions();
options.AddArgument("force-device-scale-factor=1");
//不需添加额外属性 options可不写 直接 var driver = new EdgeDriver()
using (var driver = new EdgeDriver(options))
{
//打开指定Url路径
driver.Navigate().GoToUrl("https://test.com");
//将浏览器全屏化
driver.Manage().Window.Maximize();
try
{
/可添加等待时间 等待网页加载 直至某元素出现
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
//等待直到标题出现
//wait.Until(d=>d.Title.Contains("Example Domain"));
//等待特定元素出现
//等待Test按钮出现
wait.Until(d => d.FindElement(By.Id("test-button")).Displayed);
//查找按钮元素并点击
IWebElement buttonElement = driver.FindElement(By.Id("test-button"));
buttonElement.Click();
//网页跳转后 等待网页加载
System.Threading.Thread.Sleep(1000);
//直到某元素出现 by -name
wait.Until(d => d.FindElement(By.Name("username")).Displayed);
//by -css
//wait.Until(d => d.FindElement(By.CssSelector("input[class='test']")).Displayed);
// wait.Until(d => d.FindElement(By.XPath("//input[@name='test 1 color 1']")).Displayed);
//获取class包含test的元素
IWebElement input_name = driver.FindElement(By.CssSelector("input[class*='test']"));
//往input框赋值
input_name.SendKeys("test");
//不好获取ID、Name、Class时,可采用XPath方式
wait.Until(d => d.FindElement(By.XPath("//button[@type='submit']")).Displayed);
//wait.Until(d => d.FindElement(By.CssSelector("input[type='submit']")).Displayed);
//wait.Until(d => d.FindElement(By.ClassName("test")));
buttonElement = driver.FindElement(By.XPath("//button[@type='submit']"));
buttonElement.Click();
//Radiobutton 的获取
//wait.Until(d => d.FindElement(By.XPath("//input[@type='type1' and @name='name' and @value='1']")).Displayed);
//IWebElement radio_name = driver.FindElement(By.XPath("//input[@type='type' and @name='name' and @value='1']"));
// radio_name.Click();
//By Class 获取按钮
wait.Until(d => d.FindElement(By.ClassName("test-button")).Displayed);
buttonElement = driver.FindElement(By.ClassName("test-button"));
buttonElement.Click();
//By XPath 获取a 、i 标签
//wait.Until(d => d.FindElement(By.XPath("//i[@class='tets test-color-green test-status-ok-core']")).Displayed);
//wait.Until(d => d.FindElement(By.XPath("//a[@Test-click='redirectToselectedTest(TestStatusSystem.id)']")).Displayed);
IWebElement test_div = driver.FindElement(By.Id("test_div"));
//获取test_div中的所有div
//var div_count = test_div.FindElements(By.TagName("div"));
//只获取test_div里最外层div
var div_count = sys_div.FindElements(By.XPath("./div"));
//获取Strong元素的外层a标签方法
wait.Until(d => d.FindElement(By.XPath("//strong[@id='test']/parent::a")).Displayed);
buttonElement = driver.FindElement(By.XPath("//strong[@id='test']/parent::a"));
buttonElement.Click();
//执行JavaScript脚本来滚动页面
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.scrollTo(0,document.body.scrollHeight)");
//下拉到指定标签位置 如: label translate="test"
IWebElement targetElement = driver.FindElement(By.XPath("//label[@test']"));
js.ExecuteScript("arguments[0].scrollIntoView(true);", targetElement);
//勾选CheckBox
//wait.Until(d => d.FindElement(By.Id("cb_ck")).Displayed);
//IWebElement checkbox = driver.FindElement(By.Id("cb_ck"));
//if (checkbox.Selected == false)
//{
// checkbox.Click();
//}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
版权归原作者 AI程序猿 所有, 如有侵权,请联系我们删除。