【问题标题】:java.net.MalformedURLException in Java Selenium codeJava Selenium 代码中的 java.net.MalformedURLException
【发布时间】:2017-05-17 09:19:40
【问题描述】:

我尝试运行简单的 Java Selenium 代码,但出现此错误 - 谁能帮我解决这个问题?

import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class test
{
public static void main(String[] args)
{
stem.setProperty("webdriver.chrome.driver","D:/apache-jmeter-3.1/bin/chromedriver.exe");
WebDriver driver = new ChromeDriver();       
driver.get("https://www.google.com/");
String Title = driver.getTitle();

//compare the actual title of the page with the expected one
if (Title.contentEquals("Google"))
{
System.out.println("Test Passed!");
}
else
{
System.out.println("Test Failed");
}
driver.close();
}

}

【问题讨论】:

  • 表示网址无效。网址是什么?
  • 我已将链接传递为 driver.get("google.com");
  • 看来你写的是 'stem' 而不是 'System'

标签: java selenium automation jmeter jmeter-plugins


【解决方案1】:

您似乎在get() 方法中使用了不正确的网址。尝试使用get() 方法如下:

driver.get("http://www.google.com");

URL 必须包含“http://”或“https://”来定义其协议。

修复您的代码,您可以在WebDriver Sampler 中尝试以下一次:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class test {
  public static void main(String[] args) { 
   try{
    System.setProperty("webdriver.chrome.driver","D:/apache-jmet‌​er-
      3.1/bin/chromedri‌​ver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.google.com");
    String Title = driver.getTitle();
    if (Title.contentEquals("Google")){
      System.out.println("Test Passed!");
    } else {
      System.out.println("Test Failed");
    } 
    driver.close();
  } catch (Exception e){}
}

}

【讨论】:

  • @PrashanthNagendra,请张贴 WebDriver Sampler 的截图。
  • javax.script.ScriptException:源文件:内联评估:``import org.openqa.selenium.WebDriver;导入 org.openqa.selenium.chrome.ChromeDr 。 . . '' 令牌解析错误:
  • 先生,我用异常处理代码试过了,没用
  • @PrashanthNagendra 请使用此代码。请注意 setProperty 中的双斜杠。谢谢! System.setProperty("webdriver.chrome.driver", "C:\\pathto\\my\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://www.google.com");
【解决方案2】:

使用这个:

System.setProperty("webdriver.chrome.driver","D://apache-jmeter-3.1//bin//chromedriver.exe");

如 System 类的 javadoc 中所述:

设置由指定键指示的系统属性。

首先,如果存在安全管理器,它的 SecurityManager.checkPermission 方法被调用 PropertyPermission(key, "write") 权限。这可能会导致 抛出 SecurityException。如果没有抛出异常,则 指定的属性设置为给定的值。

参数:

key - 系统属性的名称。价值 - 的价值 系统属性。

返回:

系统属性的前一个值,如果没有,则返回 null 一。抛出:SecurityException - 如果存在安全管理器并且 它的 checkPermission 方法不允许设置指定的 财产。 NullPointerException - 如果键或值为空。 IllegalArgumentException - 如果键为空。

【讨论】:

  • 请解释原因
  • 我不明白没有内容要解释的答案。我可能会标记这个答案。
猜你喜欢
  • 2017-06-07
  • 2012-08-26
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-02
相关资源
最近更新 更多