我有一个String变量,可以从Excel中获取一些ID。
String answerID = formatter.formatCellValue(sheet.getRow(i).getCell(2));
每次使用ID时,我都在使用JavaScript执行器来更改ID,但似乎串联不正确:
je.executeScript("document.querySelector('.js-react-thanks-button-answer-id-"+answerID+" button').click()");
System.out.println("Thank you checked for link " + link);
je.executeScript("document.querySelector('div[data-answer-id='" + answerID + "'] .sg-rate-box__filled-stars span:last-of-type svg').click()");
System.out.println("5 stars for link " + link);
能给我一点帮助吗?
答案 0 :(得分:0)
您需要按如下所示使用斜杠(\)对符号'进行转义
je.executeScript("document.querySelector('div[data-answer-id=\'" + answerID + "\'] .sg-rate-box__filled-stars span:last-of-type svg').click()");
System.out.println("5 stars for link " + link);
答案 1 :(得分:0)
首先在pom中添加以下依赖项:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
,然后按照以下代码进行操作:
js.executeScript(StringEscapeUtils.unescapeEcmaScript("document.querySelector('.js-react-thanks-button-answer-id-"+answerID+" button').click()"));
js.executeScript(StringEscapeUtils.unescapeEcmaScript("document.querySelector('div[data-answer-id='" + answerID + "'] .sg-rate-box__filled-stars span:last-of-type svg').click()"));
希望对您有帮助。
对于转义的javascript,您可以使用:
js.executeScript(StringEscapeUtils.escapeEcmaScript("document.querySelector('.js-react-thanks-button-answer-id-"+answerID+" button').click()"));
js.executeScript(StringEscapeUtils.escapeEcmaScript("document.querySelector('div[data-answer-id='" + answerID + "'] .sg-rate-box__filled-stars span:last-of-type svg').click()"));