Python Selenium无法定位Linkvertise iframe内指定元素求助
Python Selenium无法定位Linkvertise创建链接页面的描述输入框
我一直在尝试用Python Selenium自动化完成Linkvertise广告链接的创建流程,试了所有能想到的办法,还在多个论坛甚至付费平台求助,但始终找不到解决办法。
当前使用的代码如下:
from time import sleep from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument("user-data-dir=selenium") browser = webdriver.Chrome(options=chrome_options) link_to_publish = "https://random.com/2937" browser.get("https://publisher.linkvertise.com/dashboard#link-create") # 填写要推广的链接 browser.switch_to.frame("link-create-iframe") browser.find_element(By.ID, "mat-input-0").send_keys(link_to_publish) # 点击继续按钮 sleep(3) browser.find_element(By.CSS_SELECTOR, "body > app-root > lv-link-create-page-component > div > lv-lm-page > div > div.col.p-0 > div > lv-lm-button-wrapper > div > lv-button.mat-tooltip-trigger > a").click() # 关闭可见性开关 sleep(3) browser.find_element(By.CSS_SELECTOR, "#mat-slide-toggle-2 > label > div").click() # 尝试定位标题输入框(此处已卡住,实际目标是下方的描述输入框) sleep(3) browser.find_element(By.ID, 'mat-input-8')
首次运行需要手动登录,登录会话会保存在当前目录,后续运行可自动跳过登录步骤。代码里保留了sleep延时来放慢流程,避免加载问题。
问题详情
目标元素是创建链接流程第二页底部的链接描述输入框,该页面位于link-create-iframe内。我已经能成功定位并操作这个iframe里的其他元素(包括同一页面的可见性开关),但唯独找不到这个描述输入框。
目标元素的HTML代码如下:
<input lvpreventdoublespacesdoublenewlines="" autocomplete="off" formcontrolname="btn_text" matinput="" spellcheck="false" class="mat-input-element mat-form-field-autofill-control ng-touched ng-pristine ng-invalid cdk-text-field-autofill-monitored highlight-textarea" maxlength="50" placeholder="Beschreibe deinen Link präzise in wenigen Worten" required="" id="mat-input-22" data-placeholder="Beschreibe deinen Link präzise in wenigen Worten" aria-required="true">
已尝试的解决方法
- 延长sleep时长至10秒以上,排除内容未加载完成的可能
- 退出iframe后尝试定位,确认元素不在主文档内
- 分别用ID、类名、CSS选择器、XPath尝试定位元素
如果有人愿意帮忙排查,我可以提供测试账号:
- 邮箱:pschw1@tellofon.com
- 密码:Pschw123!
内容的提问来源于stack exchange,提问作者user21079650
相关产品推荐
相关产品推荐

