カール:
私はCSSセレクタによってウェブ要素を見つけるために、セレンを使用しています。とき、私はハードコードに、それは以下のように細かい動作します。
driver.findElement(By.cssSelector("div[aria-label=\"2018-10-17 Shared Google Drive Folder\"]")).getAttribute("data-id");
私は以下のような日付に基づいてCSSセレクタ文字列をカスタマイズしたい場合は、それはエラーがスローされます。
org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified
私はcssFormatStringをプリントアウトし、それは、上記のハードコーディングされたものとまったく同じに見えます。それは間違っていたところ、誰が教えてもらえますか?
// Customized cssFormatString code
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String strDate = dateFormat.format(date);
String cssFormatString = "div[aria-label=\\\"" + strDate + " Shared Google Drive Folder\\\"]";
driver.findElement(By.cssSelector(cssFormatString)).getAttribute("data-id");
Nvrsu:
単に代わりに二重引用符の単一引用符を使用し、それが動作するはずです。
String cssFormatString = "div[aria-label='2018-10-17 Shared Google Drive Folder']"
、あなたの文字列の連結のは簡単になりますので、
String cssFormatString = "div[aria-label='" + strDate + " Shared Google Drive Folder']"