如何使用 Selenium 和 Java 单击元素
How to click on the element using Selenium and Java
https://shop.medtronic-diabetes.ch/
在上面的站点中,单击右上角的 Anmelden link,倒数第三个。
我无法找到电子邮件 xpath,因为它是框架的一部分。
尝试了以下但没有成功
driver.switchTo().frame(0);
driver.switchTo().frame("cbox1556878105885");
driver.switchTo().frame("cboxIframe");
driver.switchTo().frame(find_element_by_xpath(//xpath));
单击位于右上角的文本为 Anmelden 的元素,即倒数第三项,然后发送 字符序列 到 Email 字段,您需要引入 WebDriverWait 以使 元素可点击 并且您可以使用以下任一解决方案:
cssSelector
:
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.FrameToBeAvailableAndSwitchToIt
。
如果您有 iframe 的 ID,请使用 Driver.SwitchTo().Frame("frameID");
对于 Select 页面上的第一个框架或主文档,使用 Driver.SwitchTo().DefaultContent();
或对于 Select 父框架,使用 Driver.SwitchTo().ParentFrame();
https://shop.medtronic-diabetes.ch/
在上面的站点中,单击右上角的 Anmelden link,倒数第三个。 我无法找到电子邮件 xpath,因为它是框架的一部分。
尝试了以下但没有成功
driver.switchTo().frame(0);
driver.switchTo().frame("cbox1556878105885");
driver.switchTo().frame("cboxIframe");
driver.switchTo().frame(find_element_by_xpath(//xpath));
单击位于右上角的文本为 Anmelden 的元素,即倒数第三项,然后发送 字符序列 到 Email 字段,您需要引入 WebDriverWait 以使 元素可点击 并且您可以使用以下任一解决方案:
cssSelector
: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.FrameToBeAvailableAndSwitchToIt
。
如果您有 iframe 的 ID,请使用 Driver.SwitchTo().Frame("frameID");
对于 Select 页面上的第一个框架或主文档,使用 Driver.SwitchTo().DefaultContent();
或对于 Select 父框架,使用 Driver.SwitchTo().ParentFrame();