亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Selenium 和 Java 通過 sendKeys 方法傳遞雙引號字符串

如何使用 Selenium 和 Java 通過 sendKeys 方法傳遞雙引號字符串

慕仙森 2023-07-28 16:46:06
我正在編寫一個 selenium 腳本,其中正在獲取 3 個值并將其存儲在 String 中。String one = "19292";String two = "Abc";String three = "def";我希望它以(一+二+三)的形式發送文本,但它們都帶有雙引號。所以最終結果應該是"19292""Abc""def"我怎樣才能做到這一點 ?我嘗試過使用反斜杠來使用轉義機制,但是每當我使用它而不是獲取字符串值時,它都會打印文本。對于例如:\"one\" prints "one" rather than "19292"
查看完整描述

4 回答

?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

嘗試這樣:


field.sendKeys("\"" + one + "\"\"" + two + "\"\"" + three + "\"");

我剛剛檢查了硒,它有效。進入字段的輸入是:“19292”“Abc”“def”


或者,如果您不知道字符串的數量,那么下面是將轉換為引號格式的方法。


public static void main(String[] args) {

    String one = "19292";

    String two = "Abc";

    String three = "def";


    System.out.println(surroundWithDoubleQuotes(one, two, three));



}


public static String surroundWithDoubleQuotes(String... input) {

    if(input == null || input.length == 0) {

        return "";

    }

    StringBuilder builder = new StringBuilder();

    for(String arg : input) {

        builder.append("\"\"");

        builder.append(arg);

    }

    builder.append("\"");

    builder.deleteCharAt(0);


    return builder.toString();

}


查看完整回答
反對 回復 2023-07-28
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

嘗試:

String str = "\""+one+"\"\""+two+"\"\""+three+"\"";
System.out.println(str);

這將打印"19292""Abc""def"


查看完整回答
反對 回復 2023-07-28
?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

如果您的用例是將結果字符串作為 傳遞"19292" "Abc" "def",那么以下Strings聲明肯定不會幫助達到目的:


String one = "19292";

String two = "Abc";

String three = "def";

相反,您需要執行以下操作:


String one = " \"19292\" ";

String two = " \"Abc\" ";

String three = " \"def\" ";

使用Google 主頁的文本框"19292" "Abc" "def"將字符串作為 ** **發送的示例,您可以使用以下定位器策略:

代碼塊:


public class A_demo?

{

? ? public static void main(String[] args) throws Exception?

? ? {

? ? ? ? String one = " \"19292\" ";

? ? ? ? String two = " \"Abc\" ";

? ? ? ? String three = " \"def\" ";

? ? ? ? System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");

? ? ? ? ChromeOptions options = new ChromeOptions();

? ? ? ? options.addArguments("start-maximized");

? ? ? ? options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));

? ? ? ? options.setExperimentalOption("useAutomationExtension", false);

? ? ? ? WebDriver driver = new ChromeDriver(options);

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

? ? ? ? new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.name("q"))).sendKeys(one + two + three);

? ? }

}

瀏覽器快照:

https://img2.sycdn.imooc.com/64c380910001f35313660767.jpg

查看完整回答
反對 回復 2023-07-28
?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

嘗試這個:

String text = "\"" + one + "\"\"" + two + "\"\"" + three + "\"";


查看完整回答
反對 回復 2023-07-28
  • 4 回答
  • 0 關注
  • 246 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號