我是selenium xpath的新手,这是我尝试过的代码。它根本不进入循环。我认为路径是正确的。有人可以帮忙吗?提前致谢。
a = driver.find_elements_by_xpath('//*[@class="one"]/ul/li')
for li in a:
xval = driver.find_element_by_xpath('.div/text()')`
<div class="one">
<ul>
<li><div id="1">Hi</div></li>
<li><div id="2"> Hello</div></li>
<li><div id="3">Bye</div></li>
</ul>
</div>
答案 0 :(得分:2)
You can search child element by querying parent element. Also check the length of the parent element, does it have anything?
a = driver.find_elements_by_xpath('//*[@class="one"]/ul/li')
print len(a)
for li in a:
xval = li.find_element_by_xpath('div/text()')
答案 1 :(得分:1)
In selenium
find_element_by_xpath()
method should return WebElement
, but not text content.
Try to replace
xval = driver.find_element_by_xpath('.div/text()')
with
xval = driver.find_element_by_xpath('./div').text
Also note that you should use ./div
instead of .div
to match div
that is direct child of current element
Update
If your loop make no iteration try to wait until required div
appears in DOM
as it might be generated dynamically:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By. XPATH, "//div[@class="one" and ./ul/li])))