简体   繁体   English

如何使用 Javascript 等到元素在 Selenium 中可交互

[英]How to wait until element is interactable in Selenium using Javascript

I am trying to develop an automated test in a Single Page Application that uses VueJs, when I click on the register button the page loads a form with the elements but as the elements are loaded as needed they are not present there automatically I can solve the problem placing a driver.sleep after the registration button is clicked but I was looking for a workaround I tried ImplicitWaits and elementIsEnabled but I couldn't get results, check my code here:我正在尝试在使用 VueJs 的单页应用程序中开发自动化测试,当我单击注册按钮时,页面会加载包含元素的表单,但是当元素根据需要加载时,它们不会自动出现在那里我可以解决单击注册按钮后放置 driver.sleep 时出现问题,但我正在寻找一种解决方法我尝试了 ImplicitWaits 和 elementIsEnabled 但我无法获得结果,请在此处检查我的代码:

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

async function teste() {异步 function 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()睾丸()

async function robo(element, driver, TIMEOUT=10000){异步 function 机器人(元素,驱动程序,TIMEOUT=10000){

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

} }

This is a Java solution.这是一个 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;
    }
}

Please do similar with JS.请对 JS 做类似的事情。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM