我正在尝试从xpath中获取元素,这里是代码,为了避免上百万行,我将其中的一些元素削减了代码。
html
<keyword-text class="_nghost-fyp-81"><div class="keyword-text _ngcontent-
fyp-81" clickabletooltiptarget="" aria-label=""><span class="keyword
_ngcontent-fyp-81" aria-hidden="false">new york new york las vegas</span>
<!----></div><!----><!----></keyword-text>
xpath
keyword_text = self.browser.find_elements_by_xpath("//span[starts-with(@class, 'keyword')]")
错误
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="0f74389d-13b7-42e8-ba81-429388d6647c")>
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="edc98646-f4e3-4ec9-8307-c12c110a219a")>
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="a6e12a98-22f4-4b1b-aad1-4831dcdb00a0")>
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="89f50425-8d53-4292-8e33-2a268f62c8b1")>
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="d7af6e64-6aa9-4fd0-9f9e-034f56d69c0d")>
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="1566c190-3809-4c40-b0c7-1051502bc658")>
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="f8304581-fba4-42c9-8332-8fbe5f05daf7")>
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="b6d79b66-874e-4233-9100-af4242a34c1b")>
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="a21f9c54-0dbf-4e4c-8055-f007c1b26c80")>
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="1e3dcbd6-a2de-4335-8688-f5dd5391d5dd")>
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="61bff097-3394-4b90-93e7-c5aeb199321b")>
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="ddc2fe2b-1d50-4e6c-909f-3f47c704a36d")>
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="0061e31c-3607-43c3-a984-c28130771119")>
答案 0 :(得分:1)
以下控制台消息...
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="0f74389d-13b7-42e8-ba81-429388d6647c")>
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="edc98646-f4e3-4ec9-8307-c12c110a219a")>
<selenium.webdriver.remote.webelement.WebElement (session="941dc1eba2f3f61bb47aa2050889d299", element="a6e12a98-22f4-4b1b-aad1-4831dcdb00a0")>
...不是这样的错误。
使用 find_elements*
时,得到的keyword_text
是 WebElements 类型的对象的列表,当您打印它们时,这些对象被打印出来。
要打印文本,例如纽约拉斯维加斯,您必须为visibility_of_all_elements_located()
引出 WebDriverWait 这几个要素,并且可以使用以下{{3} }:
使用CSS_SELECTOR
:
print([my_elem.text for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "keyword-text>div.keyword-text>span.keyword")))])
使用XPATH
:
print([my_elem.text for my_elem in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//keyword-text/div[contains(@class, 'keyword-text')]/span[contains(@class, 'keyword')]")))])
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC