这是 html 元素
<div class="mat-form-field-infix ng-tns-c64-9"><!----><input _ngcontent-acc-c94="" matinput="" readonly="true" class="mat-input-element mat-form-field-autofill-control cdk-text-field-autofill-monitored ng-star-inserted" style="display: none;" id="mat-input-0" aria-invalid="false" aria-required="false"><!----><!----><span _ngcontent-acc-c94="" class="ng-star-inserted"><span _ngcontent-acc-c94="" class="ng-star-inserted"><a _ngcontent-acc-c94="" target="_blank" rel="noopener" href="mailto:[email protected]">[email protected]</a></span><!----><!----></span><!----><span class="mat-form-field-label-wrapper ng-tns-c64-9"><label class="mat-form-field-label ng-tns-c64-9 ng-star-inserted" id="mat-form-field-label-1" for="mat-input-0" aria-owns="mat-input-0"><!----><mat-label _ngcontent-acc-c94="" class="ng-tns-c64-9 ng-star-inserted">E-mail</mat-label><!----><!----></label><!----></span></div>
我的代码:
owner_id=driver.find_element(By.XPATH,'')
owner_id=driver.find_element(By.CSS_SELECTOR,'')
我尝试使用 xpath、css 选择器、类名进行提取,但没有任何效果。如果你们知道请帮帮我
我尝试使用您发布的 Html 源,我可以使用以下代码获取电子邮件 ID:
print(driver.find_element(By.CSS_SELECTOR, ".mat-form-field-infix.ng-tns-c64-9 span a").text)
输出:
[email protected]
使用
WebDriverWait()
并等待元素成为 visible
并跟随 xpath
。
owner_id=WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//span[contains(., 'E-mail')]/preceding-sibling::span[1]//a")))
print(owner_id.text)
print(owner_id.get_attribute("textContent"))
导入以下库
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By