难以将 Selenium 指向正确的 iFrame [python]
Having difficulty pointing Selenium towards the right iFrame [python]
我正在尝试让我的 selenium 脚本从 MTurk HIT 下载图像。我的脚本能够登录到 MTurk,转到我想从中获取图像的 HIT 的 "accept a new 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
我期望的输出:
HTML内linkhttps://backend.ibotta.com/receipt_moderation/50730299/edit?assignmentId=33FBRBDW6OZTOIJ53FZR716JLOQC8N&hitId=3D3B8GE892RAASDPNAMA2D4I3E3P9G&workerId=A1DY4DM16TBFPL&turkSubmitTo=https%3A%2F%2Fwww.mturk.com
我要访问的元素在页面源代码中称为 ExternalQuestionIFrame
,如下所示:
</style><iframe height="1000" scrolling="auto" frameborder="0" align="center" src="https://backend.ibotta.com/receipt_moderation/50730299/edit?assignmentId=33FBRBDW6OZTOIJ53FZR716JLOQC8N&hitId=3D3B8GE892RAASDPNAMA2D4I3E3P9G&workerId=A1DY4DM16TBFPL&turkSubmitTo=https%3A%2F%2Fwww.mturk.com" name="ExternalQuestionIFrame"></iframe>
谁能看出我错在哪里?非常感谢任何回复!
您无需切换到 iframe 即可获得它 src
。只需定位元素并使用 get_attribute()
检索 src
属性值:
frame = driver.find_element_by_name("ExternalQuestionIFrame")
print(frame.get_attribute("src"))
我正在尝试让我的 selenium 脚本从 MTurk HIT 下载图像。我的脚本能够登录到 MTurk,转到我想从中获取图像的 HIT 的 "accept a new 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
我期望的输出:
HTML内linkhttps://backend.ibotta.com/receipt_moderation/50730299/edit?assignmentId=33FBRBDW6OZTOIJ53FZR716JLOQC8N&hitId=3D3B8GE892RAASDPNAMA2D4I3E3P9G&workerId=A1DY4DM16TBFPL&turkSubmitTo=https%3A%2F%2Fwww.mturk.com
我要访问的元素在页面源代码中称为 ExternalQuestionIFrame
,如下所示:
</style><iframe height="1000" scrolling="auto" frameborder="0" align="center" src="https://backend.ibotta.com/receipt_moderation/50730299/edit?assignmentId=33FBRBDW6OZTOIJ53FZR716JLOQC8N&hitId=3D3B8GE892RAASDPNAMA2D4I3E3P9G&workerId=A1DY4DM16TBFPL&turkSubmitTo=https%3A%2F%2Fwww.mturk.com" name="ExternalQuestionIFrame"></iframe>
谁能看出我错在哪里?非常感谢任何回复!
您无需切换到 iframe 即可获得它 src
。只需定位元素并使用 get_attribute()
检索 src
属性值:
frame = driver.find_element_by_name("ExternalQuestionIFrame")
print(frame.get_attribute("src"))