问题
I'm trying to get the elements from this xpath here is the code, there is several of these elements I cut the code down to avoid a million lines on here.
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')]")
errors
<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")>
回答1:
The following console messages...
<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")>
...are not any errors as such.
As you have used find_elements*
, the resultant keyword_text
was a list of objects of type WebElements and when you printed them, the objects were printed.
To print the texts e.g. new york new york las vegas from the several of these elements you have to induce WebDriverWait for the visibility_of_all_elements_located()
and you can use either of the following Locator Strategies:
Using
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")))])
Using
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')]")))])
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
来源:https://stackoverflow.com/questions/57943876/i-need-help-finding-an-element-with-a-locator