我想瀏覽一個網站,找到一個元素并列印它。
Python版本:3.10;Selenium Webdriver:火狐;IDE:PyCharm 2021.3.2(CE);作業系統:Fedora 35 虛擬機
我能夠導航到在下拉選單中生成文本的相應頁面。當我通過 CSS 選擇器找到元素并嘗試列印它時,輸出會列印文本“無”。我希望它列印計劃名稱,在這種情況下是“雙重完整計劃 1 ”。該元素并不總是存在,因此我還需要捕獲任何例外。
我要列印的元素的相關 HTML 代碼:
<span class="OSFillParent" data-expression="" style="font-size: 12px; margin-top: 5px;">Dual Complete Plan 1</span>
我要列印的元素的更多 HTML 代碼(我要捕獲的元素低于第四個 div):
<td data-header="Plan Name">
<div id="b8-b40-l1_0-132_0-$b2" class="OSBlockWidget" data-block="Content.AccordionItem">
<div id="b8-b40-l1_0-132_0-b2-SectionItem" class="section-expandable open is--open small-accordion" data-container="" data-expanded="true" aria-expanded="true" aria-disabled="false" role="tab">
<div id="b8-b40-l1_0-132_0-b2-TitleWrapper" class="section-expandable-title" data-container="" style="cursor: pointer;" role "button" aria-hidden="false" aria-expanmded="true" tabindex="0" aria-controls="b8-b40-l1_0-132_0-b2-Content" EVENT FLEX
<div id="b8-b40-l1_0-132_0-b2-Title" class="dividers full-width">
<span class="OSFillParent" data-expression="" style="font-size: 12px; margin-top: 5px;">Dual Complete Plan 1</span>
</div>
<div class="section-expandable-icon" data-container="" aria-hidden="true"
::after
</div>
</div>
<div id="b8-b40-l1_0-132_0-b2-ContentWrapper" class="section-expandable-content no-padding is--expanded" data-container="" tabindex="0" aria-hidden="false" aria-labelledby="b8-b40-l1_0-132_0-b2-TitleWrapper">
<div id="b8-b40-l1_0-132_0-b2-Content" role="tabpanel">
<a data-link="" href="https://www.communityplan.com" target="_blank" title="Click for more information"> EVENT
<span class="OSFillParent" data-expression="" style="font-size: 12px;">www.CommunityPlan.com</span>
</a>
<span class="OSFillParent" data-expression="" style="font-size: 12px:">Phone Number: 8005224700</span>
</div>
</div>
</div>
</div>
</td>
我的相關硒代碼:
# Find the Plan Name & if present set it to the variable "Advantage"
try:
Advantage = (WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#b8-b40-l1_0-132_0-b2-Title > span:nth-child(1)"))).get_attribute("value"))
except:
pass
print('\033[91;46m', Advantage, '\033[0m')
I expect the output to be "Dual Complete Plan 1", which is what I see on the screen and in the HTML. Instead I get the following:
None
Apparently the "Advantage" variable is being set to "None". Why? I can see the text "Dual Complete Plan 1" that I want to print in the HTML code above. What am I doing wrong? I feel like I need a primer on "get attribute"?
uj5u.com熱心網友回復:
要獲取Dual Complete Plan 1您需要使用的文本
element.text
要么
element.get_attribute("innerHTML")
要么
element.get_attribute("textContent")
而不是presence_of_element_located()使用visibility_of_element_located()
并遵循css selector以識別
div[id*='Title'] > span.OSFillParent
要么
div.dividers.full-width > span.OSFillParent
代碼:
try:
Advantage = WebDriverWait(driver, 5).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "div[id*='Title'] > span.OSFillParent"))).text
except:
pass
print(Advantage )
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/449461.html
標籤:python selenium getattribute
上一篇:如何顯示一定數量的資訊。蟒蛇3
