难以将Selenium指向正确的iFrame [python]

时间:2015-11-22 20:34:54

标签: python selenium mechanicalturk

我正试图让我的selenium脚本从MTurk HIT下载图像。我的脚本能够登录到MTurk,转到我想从中抓取图像的HIT的“接受新的HIT”页面,但是我无法将其指向我想要的特定图像。我已经尝试了selenium文档(find_element_by_class_name, by_id, by_element)等中列出的所有方法,但我无法弄明白。

到目前为止我所拥有的:

from selenium import webdriver
from bs4 import BeautifulSoup

driver = webdriver.Firefox()

driver.get("https://www.mturk.com/mturk/myhits")

elem = driver.find_element_by_id("ap_email")
elem.send_keys('####')
elem = driver.find_element_by_id("ap_password")
elem.send_keys('###')
elem = driver.find_element_by_id("signInSubmit-input")
elem.click()
driver.get("https://www.mturk.com/mturk/previewandaccept?groupId=3ZXRRTK2NDCB5NW5M24C9P2OWG41OF")
hit = driver.switch_to_frame("ExternalQuestionIFrame")
print(hit)

输出这给了我:

None

输出我期望: 链接https://backend.ibotta.com/receipt_moderation/50730299/edit?assignmentId=33FBRBDW6OZTOIJ53FZR716JLOQC8N&hitId=3D3B8GE892RAASDPNAMA2D4I3E3P9G&workerId=A1DY4DM16TBFPL&turkSubmitTo=https%3A%2F%2Fwww.mturk.com

中的HTML

我想要覆盖的元素在页面源中称为ExternalQuestionIFrame,如下所示:

 </style><iframe height="1000" scrolling="auto" frameborder="0" align="center" src="https://backend.ibotta.com/receipt_moderation/50730299/edit?assignmentId=33FBRBDW6OZTOIJ53FZR716JLOQC8N&amp;hitId=3D3B8GE892RAASDPNAMA2D4I3E3P9G&amp;workerId=A1DY4DM16TBFPL&amp;turkSubmitTo=https%3A%2F%2Fwww.mturk.com" name="ExternalQuestionIFrame"></iframe>

谁能看到我哪里出错了?任何回复都非常感谢!

1 个答案:

答案 0 :(得分:1)

您无需切换到iframe即可获得src。只需找到该元素,然后使用get_attribute()检索src属性值:

frame = driver.find_element_by_name("ExternalQuestionIFrame")
print(frame.get_attribute("src"))