这是我在自动化中的第一个代码:
package Automationframework;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirstTestClass {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "D:\workbox\Online Store\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.store.demoqa.com");
System.out.println("Successfully opened the website www.Store.Demoqa.com");
Thread.sleep(5000);
driver.quit();
}
}
它正在抛出错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
at Automationframework.FirstTestClass.main(FirstTestClass.java:13)**
答案 0 :(得分:4)
你写了"D:\workbox\Online Store\geckodriver.exe"
\
但是字符"
在Java中具有特殊含义。它被解释为转义字符。这意味着它用于将以下字符解释为文字并删除它的特殊含义(如果有的话)。这使得可以在String
中编写符号String
,尽管它通常会引入"...\"..."
的结尾(或开头),如:
\n
它也用于编写newline
之类的特殊字符,其被解释为\w
。
根据这些知识,查看您当前的代码意味着您正在撰写\O
,\g
,\b
。但这三个都没有有效的转义序列,这就是编译器抱怨的原因。有效的转义序列是:
\t
- 退格\n
- 标签\f
- 换行符\r
- formfeed \"
- 回车\'
- 双引号字符\\
- 单引号字符\
- 反斜杠字符这就是编译器建议的原因,更多的是Data.Functor.Product.TH
。
要将\
本身用作角色,您需要再次使用\\
逃避符号,例如"D:\\workbox\\Online Store\\geckodriver.exe"
。所以通过写作
String
您收到包含文字
的D:\workbox\Online Store\geckodriver.exe
"D:/workbox/Online Store/geckodriver.exe"
如果您使用IDE,例如eclipse,您可以将其设置为在创建字符串时自动插入双斜线,非常方便。
或者您应该使用正斜杠,这些是独立于文件系统的,无论您是在Windows,Mac,Linux还是其他地方运行Java程序,它都将起作用。有关详情,请参阅official Oracle documentation。
<div class="row">
<div class="col-md-12">
<svg height="220" width="100%">
<defs>
<radialGradient id="grad1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" style="stop-color:rgb(255,255,255);
stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
</radialGradient>
</defs>
<ellipse cx="125" cy="110" rx="95" ry="85" fill="url(#grad1)" />
<defs>
<radialGradient id="grad1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" style="stop-color:rgb(255,255,255);
stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
</radialGradient>
</defs>
<ellipse cx="300" cy="110" rx="95" ry="85" fill="url(#grad1)" />
<defs>
<radialGradient id="grad1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" style="stop-color:rgb(255,255,255);
stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
</radialGradient>
</defs>
<ellipse cx="475" cy="110" rx="95" ry="85" fill="url(#grad1)" />
<defs>
<radialGradient id="grad1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" style="stop-color:rgb(255,255,255);
stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
</radialGradient>
</defs>
<ellipse cx="650" cy="110" rx="95" ry="85" fill="url(#grad1)" />
</div>
</div>
答案 1 :(得分:3)
如果你正在使用eclipse,那么有一个设置可以在粘贴时自动插入转义字符:
窗口 - &gt;偏好 - &gt; Java - &gt;编辑 - &gt;打字 - &gt;在字符串中 文字 - &gt;粘贴到字符串文字时转义文本
然后,当你的剪贴板中有D:\Env\Images\image1.png
这样的内容并将其粘贴到eclipse中时,它会自动显示如下:D:\\Env\\Images\\image1.png
答案 2 :(得分:0)
指定路径时使用双(向后)斜杠:
"D:\\workbox\\Online Store\\geckodriver.exe"