0

我希望能够单击同意按钮以同意网站上的 cookie,我知道如何在 selenium webdriver 中执行此操作,但是,我不知道如何使用 js 和 mocha 执行此操作,因为我正在尝试学习任何帮助表示赞赏。

我试过了

browser.switchToFrame($('#sp_message_iframe_207015')); 
$(getHighlightedText('Agree')).click();

但是没用

基本上,我启动该网站,然后弹出一个询问同意 cookie 并管理 cookie/首选项的弹出窗口,我只想能够点击同意

#sp_message_iframe_207015是 iframe 的 ID

同意元素看起来像这样

<button 
    tabindex="0" 
    title="Agree" 
    aria-label="Agree" 
    class="message-component message-button no-children" 
    path="[0,4,1]" 
    style="padding: 10px 50px; margin: 10px; border-width: 1px; border-color: rgb(0, 115, 197); border-radius: 20px; border-style: solid; font-size: 14px; font-weight: 600; color: rgb(255, 255, 255); font-family: &quot;trebuchet ms&quot;, helvetica, sans-serif; width: auto; background: rgb(0, 115, 197);"
>
    Agree
</button>
4

2 回答 2

3

谢谢大家

我设法让它与以下工作

let frame= browser.$('#sp_message_iframe_207015');      
 browser.pause(5000);
 browser.switchToFrame(frame);
 browser.setTimeout({ 'implicit': 10000 })     
 let clickAgree =   $('button[title="Agree"]');   
 clickAgree.click();
 browser.switchToParentFrame();
于 2021-01-26T22:06:56.350 回答
0

场景如下:

  1. 确保 iframe 存在
  2. 使用browser.switchToFrame切换到 iframe
  3. 如果有嵌套的 iframe,请重复步骤 1 - 2
  4. 在 iframe 中查找元素

就我无法访问您的应用而言,让我们想象一下 DOM 结构

body
  h1
  iframe id="message"
    h2
    <button title="Agree">Agree</button>
  div
// switch to iframe
const mainIframe = $('#main')
expect(mainIframe).toExist()
browser.switchToFrame(mainIframe)

// interact with element within iframe
const agreeButton = $('button[title="Agree"]')
expect(agreeButton).toBeClickable()
agreeButton.click()

// switch back to parent frame
browser.switchToParentFrame()

这是接受 cookie 并使用 webdriverio 浏览器对象切换到 iframe 的示例 如何检查视频是否正在播放

于 2021-01-25T15:05:05.657 回答