等到输入readonly属性消失

时间:2017-06-19 10:24:28

标签: c# selenium

在加载页面时,我的输入具有'readonly'属性。如何检查此属性是否已被删除?我正在使用Selenium和C#

我的代码:

var aws = require("aws-sdk");

exports.handler = function(event, context) {
    console.log("REQUEST RECEIVED:\n" + JSON.stringify(event));

    // For Delete requests, immediately send a SUCCESS response.
    if (event.RequestType == "Delete") {
        sendResponse(event, context, "SUCCESS");
        return;
    }

    const iot = new aws.Iot();
    iot.describeEndpoint({}, (err, data) => {
    let responseData, responseStatus;
        if (err) {
            responseStatus = "FAILED";
            responseData = { Error: "describeEndpoint call failed" };
            console.log(responseData.Error + ":\n", err);
        } else  {
            responseStatus = "SUCCESS";
            responseData = { IotEndpointAddress: data.endpointAddress };
            console.log('response data: ' + JSON.stringify(responseData));
        }

        sendResponse(event, context, responseStatus, responseData);
    });
};

// Send response to the pre-signed S3 URL 
function sendResponse(event, context, responseStatus, responseData) {

    var responseBody = JSON.stringify({
        Status: responseStatus,
        Reason: "See the details in CloudWatch Log Stream: " + context.logStreamName,
        PhysicalResourceId: context.logStreamName,
        StackId: event.StackId,
        RequestId: event.RequestId,
        LogicalResourceId: event.LogicalResourceId,
        Data: responseData
    });

    console.log("RESPONSE BODY:\n", responseBody);

    var https = require("https");
    var url = require("url");

    var parsedUrl = url.parse(event.ResponseURL);
    var options = {
        hostname: parsedUrl.hostname,
        port: 443,
        path: parsedUrl.path,
        method: "PUT",
        headers: {
            "content-type": "",
            "content-length": responseBody.length
        }
    };

    console.log("SENDING RESPONSE...\n");

    var request = https.request(options, function(response) {
        console.log("STATUS: " + response.statusCode);
        console.log("HEADERS: " + JSON.stringify(response.headers));
        // Tell AWS Lambda that the function execution is done  
        context.done();
    });

    request.on("error", function(error) {
        console.log("sendResponse Error:" + error);
        // Tell AWS Lambda that the function execution is done  
        context.done();
    });

    // write data to request body
    request.write(responseBody);
    request.end();
}

此代码有效,但我认为还有更合适的方法。

3 个答案:

答案 0 :(得分:1)

我没有看到任何其他方法使这段代码比删除inputReadOnly变量更好。 如果你不在其他任何地方使用它,你可以用这个替换你的while循环:

while (input.GetAttribute("readonly") == "true")
{
    // maybe do a thread.sleep(n_milliseconds);
}

希望这有帮助。

答案 1 :(得分:1)

最好的方法是使用名为“wait”的内置Selenium功能。我使用此代码已超过6个月没有任何问题。

第1步:创建扩展方法。

 private static WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
 public static void WaitUntilAttributeValueEquals(this IWebElement webElement, String attributeName, String attributeValue)
    {           
            wait.Until<IWebElement>((d) =>
            {
                //var x = webElement.GetAttribute(attributeName); //for debugging only
                if (webElement.GetAttribute(attributeName) == attributeValue) 
                {
                    return webElement;
                }
                return null;
            });
        }

第2步:使用

IWebElement x = driver.FindElement(By.ClassName("myInput")) // Initialization
x.WaitUntilAttributeValueEquals("readonly",null)
input.SendKeys("Text");

说明:此代码将在20秒内检查每500ms(这是'wait'方法的默认行为),无论指定{{1}的“readonly”属性等于null。如果在20秒之后,它仍然不是IWebElement,则抛出异常。当值已更改为null时,将执行下一个代码行。

答案 2 :(得分:0)

您可以通过执行类似

的操作来减少行数
IWebElement input = driver.FindElement(By.ClassName("myInput"));
while (input.GetAttribute("readonly") == "true");
input.SendKeys("Text");

您可能还希望限制等待此时间以避免无限循环的时间

IWebElement input = driver.FindElement(By.ClassName("myInput"));

Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();

while (input.GetAttribute("readonly") == "true" && stopwatch.Elapsed.TotalSeconds < timeoutInSeconds);

input.SendKeys("Text");