【问题标题】:How to click on the element using Selenium and Java [closed]如何使用 Selenium 和 Java 单击元素 [关闭]
【发布时间】:2019-05-03 09:54:57
【问题描述】:

https://shop.medtronic-diabetes.ch/

在上面的网站中,点击右上角的 Anmelden 链接,倒数第三个。 我无法找到电子邮件 xpath,因为它是框架的一部分。

尝试了以下但没有运气

  1. driver.switchTo().frame(0);
  2. driver.switchTo().frame("cbox1556878105885");
  3. driver.switchTo().frame("cboxIframe");
  4. driver.switchTo().frame(find_element_by_xpath(//xpath));

【问题讨论】:

  • Anmelden 不在该给定 URL 上。只需检查并更新 URL。我收到SYSTEM ERROR OCCURRED 异常
  • 是的,我也是系统错误
  • 更新了网址@cruisepandey,Adi Ohana。道歉。

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

尝试使用下面的定位器。

     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");

并选择页面上的第一个框架或主文档,使用Driver.SwitchTo().DefaultContent(); 或选择父框架,使用Driver.SwitchTo().ParentFrame();

【讨论】:

  • 抱歉,我的问题现在还不是很清楚。我实际上想在该登录弹出窗口中访问电子邮件选项。
  • @Mohit Garg 我已经编辑了答案
【解决方案2】:

点击右上角的Anmelden元素,这是倒数第三个项目,然后将字符序列发送到电子邮件 字段,您需要诱导 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");
    
  • 浏览器快照:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-08
    • 2020-03-20
    • 2016-11-11
    • 2017-06-09
    • 2020-05-02
    • 2022-11-01
    • 2021-12-28
    相关资源
    最近更新 更多