【问题标题】:How to send text to the username and password field within the webpage throug Safari Browser and selenium-webdriver如何通过 Safari 浏览器和 selenium-webdriver 将文本发送到网页中的用户名和密码字段
【发布时间】:2018-09-10 18:02:18
【问题描述】:

我正在尝试登录我应该为其编写测试脚本的网页。但是登录脚本每次在 Safari 中都会失败,尽管相同的脚本在 Chrome 上运行良好。

显示的错误信息:

Sep 10, 2018 10:55:06 AM org.openqa.selenium.support.ui.ExpectedConditions findElement
WARNING: WebDriverException thrown by findElement(By.id: mfacode)
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:05:20.749Z'
System info: host: 'iMac.localdomain', ip: 'fe80:0:0:0:1c2b:a0b9:a043:3a94%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.6', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.safari.SafariDriver
Capabilities {applicationCacheEnabled: true, browserName: safari, cleanSession: true, cssSelectorsEnabled: true, databaseEnabled: true, handlesAlerts: true, javascriptEnabled: true, locationContextEnabled: false, nativeEvents: true, platform: MAC, platformName: MAC, rotatable: false, version: 13605.3.8, webStorageEnabled: true}
Session ID: E2219A59-8EEE-4380-93B6-77A7DDE289BE
*** Element info: {Using=id, value=mfacode}

我正在使用的脚本: 公共类 LoginSafari {

public static void main(String[] args) {

    System.setProperty("webdriver.safari.driver", "/usr/bin/safaridriver");
    WebDriver driver= new SafariDriver(); 
    driver.get("https://yapiapp.io/welcome");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.className("auth0-lock-input"))).sendKeys("alaka.goswami@*****.com");
    driver.findElement(By.name("password")).sendKeys("*******");
    driver.findElement(By.className("auth0-lock-submit")).click();
    // WebDriverWait wait=new WebDriverWait(driver, 40);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);    
    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.id("******")));
    // username and password masked//

有什么办法可以通过或解决这个问题?

【问题讨论】:

    标签: selenium selenium-webdriver webdriver safaridriver webdriverwait


    【解决方案1】:

    此错误消息...

    Sep 10, 2018 10:55:06 AM org.openqa.selenium.support.ui.ExpectedConditions findElement
    WARNING: WebDriverException thrown by findElement(By.id: mfacode)
    org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. (WARNING: The server did not provide any stacktrace information)
    

    ...表示 WebDriver 实例无法根据您使用的Locator Strategy 找到任何元素。

    您需要注意以下几点:

    • 不同的浏览器呈现 HTML DOM 不同,因此您需要构造定位器策略才能工作跨浏览器
    • 由于 username/emailyour password 字段都在同一页面上,因此您只需诱导一次 WebDriverWait
    • 由于您需要调用 sendKeys() 方法,因此您需要使用 elementToBeClickable() 方法而不是 ExpectedConditions 作为 visibilityOfElementLocated()
    • 您的有效代码块将是:

      import org.openqa.selenium.By;
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.firefox.FirefoxDriver;
      import org.openqa.selenium.support.ui.ExpectedConditions;
      import org.openqa.selenium.support.ui.WebDriverWait;
      
      public class yapiapp_login {
      
          public static void main(String[] args) {
              System.setProperty("webdriver.safari.driver", "/usr/bin/safaridriver");
              WebDriver driver = new SafariDriver();
              driver.manage().window().maximize();
              driver.get("https://yapiapp.io/welcome");
              new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.auth0-lock-input[name='username  ']"))).sendKeys("alaka.goswami@*****.com");
              driver.findElement(By.cssSelector("input.auth0-lock-input[name='password']")).sendKeys("Alakananda Goswami");
          }
      }
      
    • 浏览器快照(使用 GeckoDriver/Firefox):

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      • 2019-07-15
      • 2018-12-26
      • 2018-12-20
      • 2019-03-29
      相关资源
      最近更新 更多