簡體   English   中英

如何使用 Selenium 和 Python 從 a::before 和::after 之間的列表元素中提取文本

[英]How to extract the text out of a list element between a ::before and ::after using Selenium and Python

我正在開發一個自動生成帳戶的項目。 對於 selenium 的最后一部分,我試圖從列表元素中提取文本並比較文本以確認它們匹配。 我附上了 HTML 以及到目前為止我得到的AttributeError: 'list' object has no attribute 'text'

HTML 元素

<li class="account dropdown open">

        <a id="user-salutation" href="#" class="dropdown-toggle" data-toggle="dropdown" segment-event="Expanded User Profile Menu" segment-ui-location="Global Nav" aria-expanded="true"> == 0
                        ::before
                       Hi TQLJOlsen1</a>
                             ::after
        <ul class="dropdown-menu" id="userPreferencesUl">
            <li class="larger"><a class="userPreferencesTrigger" title="Settings">Settings</a></li>
            <li class="larger"><a target="_blank" href="https://account.dat.com?source=Power" title="Account">Account</a></li>
            <li class="larger compactView">
                <a compact-view-toggle="" compact-text="Compact View" normal-text="Normal View" segment-event="Applied [title]" segment-ui-location="Profile" class="ng-isolate-scope" title="Compact View">Compact View</a>
            </li>
            <li class="larger"><a id="changePassword" target="_blank" href="https://account.dat.com?source=Power" title="Change Password">Change Password</a></li>
            <li><hr></li>
            <li class="larger">
                <a id="logout" onclick="logout('/logout');" title="Log Out" segment-event="Logged Out" segment-ui-location="Profile" segment-reset="true">Log Out</a>
            </li>
        </ul>
</li>

HTML 為特定元素

<a id="user-salutation" href="#" class="dropdown-toggle" data-toggle="dropdown" segment-event="Expanded User Profile Menu" segment-ui-location="Global Nav" aria-expanded="true"> == $0
::before
Hi TQLJOlsen1</a>
::after

腳本

confirm = (WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, '//*[@id="user-salutation"]'))).text)
if confirm.text == ("Hi " + NewUserName):
   print("User Account Created")
else:
   print("Issue occured could not confirm user account created, please confirm creation was succesful")

Presence_of_all_elements_located()將返回一個列表,您期望WebElement在其中提取文本。 因此,您會看到錯誤:

AttributeError: 'list' object 沒有屬性 'text'


解決方案

要理想地提取文本Hi TQLJOlsen1 ,您需要為visibility_of_element_located()引入WebDriverWait ,它可以識別所需的元素,您可以使用以下任一定位器策略

  • 使用CSS_SELECTOR

     print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a#user-salutation"))).text)
  • 使用XPATH

     print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@id='user-salutation']"))).get_attribute("innerHTML"))
  • 注意:您必須添加以下導入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

您可以在如何使用 Selenium - Python 檢索 WebElement 的文本中找到相關討論

您的代碼中的行(下方)使用 EC.presence_of_all_elements_located ,它返回一個元素列表,而不是您正在尋找的一個特定元素。 這就是你得到錯誤的原因

confirm = (WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, '//*[@id="user-salutation"]'))).text)

可能的解決方案如下,

第一個解決方案 - 用下面的行替換上面的行。 通過將存在_of_all_elements_located 替換為presence_of_element_located。 請注意,這僅在以下情況下才有效

confirm = (WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="user-salutation"]'))).text)

第二種解決方案 - 向列表添加索引以獲得所需的元素。 在這里(如下所示),我們假設列表中的第一個元素是我們正在尋找的元素。 您可以更改索引並查看是否需要其他元素

confirm = (WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, '//*[@id="user-salutation"]')))[0].text)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM