很难指向正确的iFrame[python]

2024-04-28 22:33:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试让我的selenium脚本下载MTurk热门图片。我的脚本能够登录到MTurk,进入“接受新的点击”页面,找到我想要从中获取图像的点击,但是我无法将它指向我想要的特定图像。我已经尝试了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>

有人知道我错在哪里吗?任何回应都非常感谢!你知道吗


Tags: https脚本comidbydriverselenium页面
1条回答
网友
1楼 · 发布于 2024-04-28 22:33:19

不需要切换到iframe就可以得到它的src。只需找到元素并使用get_attribute()检索src属性值:

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

相关问题 更多 >