如何使用Selenium和Java单击元素
在上面的站点中,点击右上角最后三位的Anmelden链接。 我无法定位电子邮件xpath,因为它是框架的一部分 在下面尝试过,但没有运气如何使用Selenium和Java单击元素,java,selenium,xpath,css-selectors,webdriverwait,Java,Selenium,Xpath,Css Selectors,Webdriverwait,在上面的站点中,点击右上角最后三位的Anmelden链接。 我无法定位电子邮件xpath,因为它是框架的一部分 在下面尝试过,但没有运气 driver.switchTo().frame(0) driver.switchTo().frame(“cbox1556878105885”) driver.switchTo().frame(“cboxIframe”) driver.switchTo().frame(通过xpath(//xpath)查找元素) 要单击位于右上角的元素(即最后第三个项目),然后向
driver.switchTo().frame(0)代码>
driver.switchTo().frame(“cbox1556878105885”)代码>
driver.switchTo().frame(“cboxIframe”)代码>
driver.switchTo().frame(通过xpath(//xpath)查找元素)代码>
要单击位于右上角的元素(即最后第三个项目),然后向电子邮件字段发送字符序列,您需要诱导WebDriverWait使该元素可单击,并且您可以使用以下任一解决方案:
:css选择器
driver.get("https://shop.medtronic-diabetes.ch/"); new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("li.firstRow a[title='Anmelden']"))).click(); new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe.cboxIframe"))); new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.signInboxInputFields#j_username"))).sendKeys("Mohit_Garg");
:xpath
driver.get("https://shop.medtronic-diabetes.ch/"); new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@class='firstRow']//a[text()='Anmelden']"))).click(); new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='cboxIframe']"))); new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='signInboxInputFields required' and @id='j_username']"))).sendKeys("Mohit_Garg");
- 浏览器快照:
尝试使用下面的定位器
Driver.Navigate().GoToUrl("https://shop.medtronic-diabetes.ch/");
WebDriverWait webDriverWait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30));
var loginElement = webDriverWait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div[class='registerNow firstChild'] a[title='Anmelden']")));
loginElement.Click();
webDriverWait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.ClassName("cboxIframe")));
var email = webDriverWait.Until(ExpectedConditions.ElementToBeClickable(By.Id("j_username")));
email.SendKeys("Rd@test.com");
建议切换到iframe的方法是使用webdriverwait-ExpectedConditions.FrameToBeAvailable和SwitchToIt等预期条件。
如果您有iframe的ID,则使用Driver.SwitchTo().Frame(“frameID”)代码>
要选择页面上的第一帧或主文档,请使用Driver.SwitchTo().DefaultContent()代码>或要选择父帧,请使用Driver.SwitchTo().ParentFrame()代码>给定URL上不存在Anmelden。只需检查并更新URL。我收到发生系统错误
异常,我的系统错误以及更新的url@cruisepandey,Adi Ohana。抱歉,抱歉,我的问题现在还不太清楚。实际上,我想在登录弹出窗口中访问电子邮件选项。@Mohit Garg我已经编辑了答案