想要找到链接/文本并单击它

时间:2016-12-15 10:49:43

标签: python python-2.7 selenium selenium-webdriver

我使用假邮件生成工具发送邮件并点击邮件中的链接.. enter link description here

<iframe id="emailFrame" frameborder="0" width="100%" height="360px" src="http://www.fakemailgenerator.com/email/dayrep.com/testlk/message-108204614/" scrolling="no" onload="autoResize('emailFrame')">
 <html>
  <head>
   <body>
    <div>
     <p>Good day - </p>
     <p>You have been assigned an Action from the motion A Name iwhirxpppk: s.    
     </p>
     <p>Kindly follow - up on the Touchpoint Action listed below.
     </p>
     <ul>
     Please click the below link to complete your Action.
     <p>
      <a target="_blank" href="http://cfn- svr001.cloudapp.net:7100/Home/ActionResponse?eid=ygfWFB5a99mtAUQBxjNUDHjpC9AdFz/9&tpid=14iwlvior8ak6FGifOI3MSBNxnNvHiT9">Click here
      </a>
     </p>
     This email has been generated from CFN Insight by Auto man, auto@
    </div>
   </body>
  </html>
</iframe>

想要找到以下部分代码

<p>
    <a target="_blank" href="http://cfn-svr001.cloudapp.net:7100/Home/ActionResponse?eid=ygfWFB5a99mtAUQBxjNUDHjpC9AdFz/9&tpid=14iwlvior8ak6FGifOI3MSBNxnNvHiT9">Click here</a>
</p>

我尝试了所有可能的组合,但我没有任何帮助。 这是我试过的脚本

browser = webdriver.Firefox() # for b in
browser.find_element_by_id('emailFrame').find_elements_by_xp‌​ath(".//*"):
    print b # browser.find_element_by_xpath("html/body/div[1]/p[4]/a") #
    browser.find_element_by_xpath(".//*[text()='Click here']") # 
    browser.find_element_by_xpath[".//a[contains(., 'Click here')]"] # 
    browser.find_element_by_xpath(".//div[1]//p[4]/a") 
    browser.find_element_by_id('emailFrame').find_elements_by_ta‌​g_name("a"):

1 个答案:

答案 0 :(得分:0)

首先,您应切换到框架,然后在其中找到元素。

尝试如下:

from selenium import webdriver
from selenium.webdriver.common.by import By

browser = webdriver.Firefox()
browser.maximize_window()
browser.get("http://www.fakemailgenerator.com/inbox/armyspy.com/morly1985/")
frame = browser.find_element(By.ID, 'emailFrame')
browser.switch_to_frame(frame) # switch to frame by finding the iframe webelement
click_here = browser.find_element_by_xpath(".//*[text()='Click here']") # try different xpaths.
click_here.location_once_scrolled_into_view # As the element needs scroll down
click_here.click()

参考文献:

  1. https://stackoverflow.com/a/40759300/2575259
  2. http://selenium-python.readthedocs.io/navigating.html#moving-between-windows-and-frames
  3. http://selenium-python.readthedocs.io/api.html