2016-09-07 9 views
0

SeleniumとWebDriverの新機能です。 私は、このHTMLがあります。Webdriver - 要素を見つけることができません(Java)

<input id="undefined-undefined-Jobsubject-5546" type="text" value="" data-test="testing-job-subject" style="padding: 0px; position: relative; width: 100%; border: medium none; outline: medium none; background-color: transparent; color: rgb(255, 255, 255); cursor: initial; font: inherit; height: 100%; box-sizing: border-box; margin-top: 14px;"/>

を、私はこのコードを持っている:

driver.findElement(By.xpath("//input[@data-test='testing-job-subject']")); 

をが、エラーがある:

org.openqa.selenium.NoSuchElementException: Unable to locate element: //input[@data-test='testing-job-subject']

私もこれを試してみました:

driver.findElement(By.xpath("//*[starts-with(@id,'undefined-undefined-Jobsubject')]")); 

idの数値が生成されているため、By.id(....)は使用できませんが、同じエラーです。 そして、はい、タイムアウトがコードにあります。その要素はページにあります。

問題はどこですか?おかげ

答えて

2

あなたが提供する例外としてNoSuchElementExceptionを取得している場合は、次のような理由があるかもしれません: - あなたは要素を検索しようとしているとき

  • はしてもよいし、それがDOMに存在しないので、WebDriverWaitを実装して、次のように要素が表示されるまで待ちます: -

    WebDriverWait wait = new WebDriverWait(driver, 10); 
    WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']"))); 
    
  • frameまたはiframeの内部にある可能性があります。それがある場合は、以下のように要素を見つける前にそのframeまたはiframeを切り替える必要があります: -

    WebDriverWait wait = new WebDriverWait(driver, 10); 
    
    //Find frame or iframe and switch 
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name")); 
    
    //Now find the element 
    WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[data-test='testing-job-subject']"))); 
    
    //Once all your stuff done with this frame need to switch back to default 
    driver.switchTo().defaultContent(); 
    
    私は「(By.cssSelector(あなたが

    driver.findElementのために働く必要があります以下だと思う

+0

私は最初の理由を試しましたが、それでもなお何か別のエラー: "org.openqa.selenium.TimeoutException:期待された条件は失敗しました:By.cssSelectorによって見つけられた要素の可視性を待っています:input [data-test = 'testing -job-subject '](500ミリリットルの間隔で10秒間試しました) " フレームまたはiframeがありません:/ – Mephy

+0

フレームまたはiframeがないことを確認するにはどうすればいいですか? –

+0

@Mephyリンク要素のHTMLコードをここに入力してもよろしいですか?この要素がページ上に手動で表示されていることを確認してください。 –

0

これを試してみてください:

WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[data-test='testing-job-subject']"))); 
0

id * = 'Jobsubject' "));

関連する問題