Selenium (バージョン 2.28.0) を使用してサブ要素内の要素を検索しようとしていますが、selenium des はその検索をサブ要素に限定していないようです。私はこれを間違っていますか、または element.find を使用してサブ要素を検索する方法はありますか?
例として、次のコードを使用して簡単なテスト Web ページを作成しました。
<!DOCTYPE html>
<html>
<body>
<div class=div title=div1>
<h1>My First Heading</h1>
<p class='test'>My first paragraph.</p>
</div>
<div class=div title=div2>
<h1>My Second Heading</h1>
<p class='test'>My second paragraph.</p>
</div>
<div class=div title=div3>
<h1>My Third Heading</h1>
<p class='test'>My third paragraph.</p>
</div>
</body>
</html>
私のpython(バージョン2.6)コードは次のようになります:
from selenium import webdriver
driver = webdriver.Firefox()
# Open the test page with this instance of Firefox
# element2 gets the second division as a web element
element2 = driver.find_element_by_xpath("//div[@title='div2']")
# Search second division for a paragraph with a class of 'test' and print the content
print element2.find_element_by_xpath("//p[@class='test']").text
# expected output: "My second paragraph."
# actual output: "My first paragraph."
私が実行した場合:
print element2.get_attribute('innerHTML')
2 番目の区分から html を返します。したがって、セレンはその検索を要素2に限定していません。
element2 のサブ要素を見つけたいと思います。この投稿は、私のコードがSelenium WebDriver がサブ要素にアクセスすることで機能することを示唆していますが、彼の問題はタイムアウトの問題が原因でした。
ここで何が起こっているのかを理解してくれる人はいますか?