我正在编写一个脚本,该脚本将在bash shell上运行命令并打印出结果。 我将从文件A.txt中读取
A.txt
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class Playground
{
public static void main(String[] args)
{
WebDriver driver = new ChromeDriver();
driver.get("https://smnbbrv.github.io/angular-sortablejs-demo/multiple-lists");
WebElement draggable = driver.findElement(By.xpath("/html/body/app-root/div/app-multiple-lists/div[1]/div[1]/ul/li[2]"));
WebElement dropable = driver.findElement(By.xpath("/html/body/app-root/div/app-multiple-lists/div[1]/div[2]"));
// DRAW BORDERS AROUND TO MAKE SURE WE SELECT CORRECT OBJECTS
drawBorder(driver, "/html/body/app-root/div/app-multiple-lists/div[1]/div[1]/ul/li[2]");
drawBorder(driver, "/html/body/app-root/div/app-multiple-lists/div[1]/div[2]");
// TRY USING SELENIUMS ACTION CLASS
Actions actions = new Actions(driver);
actions.clickAndHold(draggable).moveToElement(dropable).release().build().perform();
// TRY AGAIN USING JAVA SCRIPT
dragAndDrop(driver, draggable, dropable);
}
public static void drawBorder(WebDriver driver, String xpath)
{
WebElement element_node = driver.findElement(By.xpath(xpath));
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].style.border='3px solid red'", element_node);
}
public static void dragAndDrop(WebDriver driver, WebElement from, WebElement to)
{
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("function createEvent(typeOfEvent) {\n" + "var event =document.createEvent(\"CustomEvent\");\n"
+ "event.initCustomEvent(typeOfEvent,true, true, null);\n" + "event.dataTransfer = {\n" + "data: {},\n"
+ "setData: function (key, value) {\n" + "this.data[key] = value;\n" + "},\n"
+ "getData: function (key) {\n" + "return this.data[key];\n" + "}\n" + "};\n" + "return event;\n"
+ "}\n" + "\n" + "function dispatchEvent(element, event,transferData) {\n"
+ "if (transferData !== undefined) {\n" + "event.dataTransfer = transferData;\n" + "}\n"
+ "if (element.dispatchEvent) {\n" + "element.dispatchEvent(event);\n"
+ "} else if (element.fireEvent) {\n" + "element.fireEvent(\"on\" + event.type, event);\n" + "}\n"
+ "}\n" + "\n" + "function simulateHTML5DragAndDrop(element, destination) {\n"
+ "var dragStartEvent =createEvent('dragstart');\n" + "dispatchEvent(element, dragStartEvent);\n"
+ "var dropEvent = createEvent('drop');\n"
+ "dispatchEvent(destination, dropEvent,dragStartEvent.dataTransfer);\n"
+ "var dragEndEvent = createEvent('dragend');\n"
+ "dispatchEvent(element, dragEndEvent,dropEvent.dataTransfer);\n" + "}\n" + "\n"
+ "var source = arguments[0];\n" + "var destination = arguments[1];\n"
+ "simulateHTML5DragAndDrop(source,destination);", from, to);
}
}
MyCode
AAA:BBB
CCC:DDD
AAA:FFF
我面临一个奇怪的问题,print命令仅适用于最后一行,其余命令将在下一行打印'%'字符,但对于最后一行则完全按预期工作。
因此,仅对A.txt的最后一行正确评估了$ result变量,而其余2行则没有任何输出。
有人可以建议我在这里做错什么吗
谢谢您的时间
答案 0 :(得分:2)
您没有删除行尾。通常,可以执行以下操作:
chomp($line);
但是,您正在unix系统上读取Windows文本文件(以CRLF结尾的行)(以LF结尾的行)。转换文件(例如,使用dos2unix
),或使用以下命令代替chomp
:
$line =~ s/\s+\z//;