[英]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
您的代码中的行(下方)使用 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.