如何使用Selenium和Python在电子邮件中定位元素

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

我想通过电子邮件激活帐户。这是我正在使用的邮件:http://www.yopmail.com/en/它是一封临时邮件。就我所知,我使用了所有方法,但是甚至找到了元素并返回了空列表。我要单击“激活锚”标记。这是结构:

    <p class="description center" style="font-family:Roboto, 'Arial Narrow', Arial;margin:0;text-align:center;color:#797979;line-height:24px;font-size:16px;margin-bottom:10px;margin-top:20px;margin-left:auto;margin-right:auto;">
    <a style="color:#f04877;" href="https://mandrillapp.com/track/click/31118617/devauth.eventjini.com?p=eyJzIjoiTFlnX0FQclZFN1d6bTNMUk9uNnFiVGxuX0QwIiwidiI6MSwicCI6IntcInVcIjozMTExODYxNyxcInZcIjoxLFwidXJsXCI6XCJodHRwczpcXFwvXFxcL2RldmF1dGguZXZlbnRqaW5pLmNvbVxcXC9hdXRoXFxcL2FzdVxcXC9QR09TVTcweS0yMDIwMDMwNl8wODQyNDFcIixcImlkXCI6XCJjYmE3OGM0OGY0OTM0MDc3ODZmMGQyYzY4YWE4YjU1ZVwiLFwidXJsX2lkc1wiOltcIjcwYjBiYTU0MGViMGI0ZWUwOTM2OThlYTU4NjQ2NDRlMWEwYmU4OTZcIl19In0" rel="nofollow">
        <b>Activate</b>
    </a>
</p>
<p class="description center" style="font-family:Roboto, 'Arial Narrow', Arial;margin:0;text-align:center;color:#797979;line-height:24px;font-size:16px;margin-bottom:10px;margin-top:20px;margin-left:auto;margin-right:auto;">OR</p>
<p class="description center" style="font-family:Roboto, 'Arial Narrow', Arial;margin:0;text-align:center;color:#797979;line-height:24px;font-size:16px;margin-bottom:10px;margin-top:20px;margin-left:auto;margin-right:auto;">
    Copy and paste the link below in your browser to activate your Eventjini ID.
</p>
                

代码试用:

 elements = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH,"/html/body/div/div[3]/div[2]/center/div/table/tbody/tr[1]/td/table/tbody/tr/td/center/p[4]/a/b")))
 for element in elements:
     print(element.text)
     if element.text == 'Activate':
         element.click()
        

//b[text()='Activate']
python selenium xpath css-selectors webdriverwait
2个回答
1
投票

您可以尝试使用XPATH单击吗?

driver.find_element_by_xpath("//a[text() = 'Activate']").click();

0
投票

所需的元素是Angular元素,因此要为可单击的元素定位要诱导WebDriverWait的元素,则可以使用以下Locator Strategies之一:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "p.description.center>a[href*='eventjini']>b"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//p[@class='description center']/a[contains(@href, 'eventjini')]/b[text()='Activate']"))).click()
    
  • :您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
© www.soinside.com 2019 - 2024. All rights reserved.