Python 中的 Selenium - 我无法从 <a> 元素中提取文本

问题描述 投票:0回答:1

我有这段Python代码:

main_div = driver.find_element(By.XPATH,"//div[@class='am-appointments am-section']")#Only One with this class
child_divs = main_div.find_elements(By.XPATH,"./div[not(contains(@class, 'am-appointments-list-head') or contains(@class, 'am-pagination am-section'))]")
time.sleep(5)
for child_div in child_divs:
appointments=child_div.find_elements(By.XPATH,"//div[@class='am-appointments-list']//div[@class='el-collapse']//div[contains(@class, 'el-collapse-item am-appointment am-back-appointment') or contains(@class, 'el-collapse-item am-appointment am-front-appointment')]")

for appointment in appointments:
appointment.click()
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='am-appointment-details']")))
phone = appointment.find_element(By.XPATH, ".//div[@class='el-col el-col-24 el-col-sm-6 el-col-lg-6']//span[@class='am-appointment-data-phone']")
phone_text = phone.text
print(f"Appuntamento Confermato! Ciao {name_text}! Ti aspetto il {appointment_date} alle {appointment_time_text} per {service_name}. Posso chiamare il {phone_text} se ho bisogno?")

我试图从包含元素的文本中提取文本并将其分配给变量“phone_text”(您可以使用文本“+393314569013”轻松找到它),但我尝试的任何操作似乎都不起作用,有人可以帮忙吗?

<div>
<div class="am-appointments-list-day-title">
<div class="el-row">
<div class="el-col el-col-24">
<h2><label class="el-checkbox"><span class="el-checkbox__input"><span class="el-checkbox__inner"></span><input
type="checkbox" aria-hidden="false" class="el-checkbox__original" value="2024-01-30"></span><span
class="el-checkbox__label">2024-01-30</span></label>
30/01/2024
</h2>
</div>
</div>
</div>
<div class="am-appointments-list">
<div role="tablist" aria-multiselectable="true" class="el-collapse">
<div class="el-collapse-item am-appointment is-active am-front-appointment">
<div role="tab" aria-controls="el-collapse-content-1185" aria-describedby="el-collapse-content-1185"
aria-expanded="true">
<div role="button" id="el-collapse-head-1185" tabindex="0" class="el-collapse-item__header is-active">
<div class="am-appointment-data">
<div class="el-row">
<div class="el-col el-col-24 el-col-lg-14">
<div class="am-appointments-flex-row-middle-align el-row"
style="margin-left: -5px; margin-right: -5px;">
<div class="el-col el-col-24 el-col-sm-4 el-col-lg-4"
style="padding-left: 5px; padding-right: 5px;"><span class="am-appointment-checkbox"><label
class="el-checkbox"><span class="el-checkbox__input"><span
class="el-checkbox__inner"></span><input type="checkbox" aria-hidden="false"
class="el-checkbox__original" value="48"></span><span
class="el-checkbox__label">48</span></label></span> <span
class="am-appointment-time pending">
09:00
</span></div>
<div class="am-appointment-id el-col el-col-24 el-col-sm-2 el-col-lg-2"
style="padding-left: 5px; padding-right: 5px;">
<p class="am-col-title">ID:</p>
<h4>48</h4>
</div>
<div class="el-col el-col-24 el-col-sm-6 el-col-lg-6"
style="padding-left: 5px; padding-right: 5px;">
<p class="am-col-title">Cliente:</p>
<h3 class="el-tooltip item" aria-describedby="el-tooltip-8529" tabindex="0"><img width="16px"
src="https://vecchiemaniere.com/wp-content/plugins/ameliabooking/public/img/group.svg"
style="display: none;"> <span class="">
James Raymon<!----></span></h3> <span>[email protected]</span> <span
class="am-appointment-data-phone">+393314569013</span> <!---->
</div>
python selenium-webdriver selenium-chromedriver
1个回答
0
投票

嘿我可以帮忙解决这个问题,

但只是为了澄清一下,以便我稍后可以更新我的答案,您正在尝试从上面的 html 代码中提取电话号码“+393314569013”。如果是这种情况,那么使用它应该可以。

phone_text = driver.find_element(By.CLASS_NAME, 'am-appointment-data- phone')
phone_text = phone_text.text

如果您可以提供您正在使用的网址,我可以测试此解决方案。如果这不是您需要的,请告诉我,我可以帮助提供更详细的答案。

为了解释上面的代码,我将变量phone_text声明为使用类名在页面上找到的元素(了解一些HTML对于学习selenium确实有好处,可以为您提供更多选择并帮助您理解网页)。然后,我将变量转换为与该元素关联的文本,这样您剩下的就是 +393314569013。

亲切的问候

莫德凯撒

© www.soinside.com 2019 - 2024. All rights reserved.