【问题标题】:How to wait until element is interactable in Selenium using Javascript如何等到元素在 Selenium 中使用 Javascript 可交互
【发布时间】:2021-03-12 20:41:04
【问题描述】:

我正在尝试在使用 VueJs 的单页应用程序中开发自动化测试,当我单击注册按钮时,页面会加载包含元素的表单,但是当元素根据需要加载时,它们不会自动出现在那里我可以解决单击注册按钮后放置 driver.sleep 的问题,但我正在寻找一种解决方法我尝试了 ImplicitWaits 和 elementIsEnabled 但我无法获得结果,请在此处查看我的代码:

const { Builder, By, Key, until } = require('selenium-webdriver')

异步函数 teste() {

const driver = await new Builder().forBrowser('chrome').build()

await driver.get('https://dez.dev.dav.med.br/login')

let element = (By.xpath('//*[contains(text(), "Faça seu cadastro")]'))
let query = await robo(element,driver)
await query.click()

await driver.sleep(2000)

element = (By.xpath('//*[contains(@title, "Nome")]'))
query = await robo(element, driver)
await query.sendKeys("VictorTesteRobo2")

element = (By.xpath('//input[contains(@title, "CPF")]'))
query = await robo(element, driver)
await query.sendKeys("11043017844")

element = (By.xpath('//*[contains(@class,"v-select__selections")]'))
query = await robo(element, driver)
await query.click()

element = (By.xpath('//*[contains(text(),"Masculino")]'))
query = await robo(element, driver)
await query.click()

element = (By.xpath('//*[contains(@title, "Data de Nascimento")]'))
query = await robo(element, driver)
await query.sendKeys("06031997")

element = (By.xpath('//*[contains(@title, "E-mail")]'))
query = await robo(element, driver)
await query.sendKeys("teste@email.com")

element = (By.xpath('//*[contains(@title, "Celular")]'))
query = await robo(element, driver)
await query.sendKeys("11995841105")

element = (By.xpath('//*[contains(text(), "Próximo")]'))
query = await robo(element, driver)
await query.click()

element = (By.xpath('//*[contains(text(), "Aceito e Cadastre-me")]'))
query = await robo(element, driver)
await query.click()

}

teste()

异步函数 robo(element, driver, TIMEOUT=10000){

    const locator = await driver.wait(until.elementLocated(element, TIMEOUT))
    
    const query = await driver.wait(until.elementIsVisible(locator, TIMEOUT))
    
    return query
    

}

【问题讨论】:

  • is clickable.Search this site with clickable and you will find solution
  • Js中的Selenium库好像没有isClickable功能

标签: javascript selenium vue.js


【解决方案1】:

这是一个 Java 解决方案。

public boolean ElementIsClickable(WebElement webElem)      
{
    try
    {
        WebDriverWait wait = new WebDriverWait(driver, 5);
        wait.until(ExpectedConditions.elementToBeClickable(webElem));
        return true;
    }
    catch (Exception e)
    {
        return false;
    }
}

请用 JS 做类似的事情。

var el = driver.wait(until.elementLocated(By.id('button'))); el.click();

【讨论】:

猜你喜欢
  • 2020-03-26
  • 2022-01-22
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 2021-08-21
  • 2020-03-08
  • 2021-12-16
相关资源
最近更新 更多