python selenium如何最好地检测闪烁文本?

时间:2016-01-18 20:27:51

标签: jquery python selenium

我正在尝试检测由此jquery导致的文本(基础文本而不管可见性)的存在:

    /* Blink thingy*/
$('.blink').each(function() {
    var elem = $(this);
    setInterval(function() {
        if (elem.css('visibility') == 'hidden') {
            elem.css('visibility', 'visible');
        } else {
            elem.css('visibility', 'hidden');
        }    
    }, 500);
});

到目前为止,我得到的结果是:

UpdateTaskCompleteElement = driver.find_element_by_xpath("/html/body/div[3]/h3/span")

UpdateTaskCompleteMessage = UpdateTaskCompleteElement.text
if UpdateTaskCompleteMessage == ExpectedUTCMessage:
     #Make a log entry
else:
     #Throw an error

在闪烁之间捕获消息的最佳方法是什么? python或selenium中是否有可以解决此问题的函数?

我一直试图将python放在一个循环中并让它运行十次,但我认为这只会降低机会,我仍然需要偶尔重新运行脚本{It' sa长脚本:(}。

3 个答案:

答案 0 :(得分:1)

在这种情况下,更好的方法是 wait for a specific element to have a specific text

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
wait.until(EC.text_to_be_present_in_element((By.XPATH, "/html/body/div[3]/h3/span"), ExpectedUTCMessage))

这将等待最多10秒每半秒检查元素中是否存在ExpectedUTCMessage文本。

答案 1 :(得分:0)

答案 2 :(得分:0)

由于答案太远了,没有其他人发布更好的东西,这是我的循环测试。我不是很喜欢这个,但确实有用。

ExpectedUTCMessage = "Whatever the message should be."
UpdateTaskCompleteElement = driver.find_element_by_xpath("/html/body/div[3]/h3/span")


BlinkDetectLoopPass = False
for BlinkChecknum in range(0,20): # I'm using 20 loops because I have seen as many as 9 failed attempts

    UpdateTaskCompleteMessage = UpdateTaskCompleteElement.text

    if UpdateTaskCompleteMessage == ExpectedUTCMessage:
        BlinkDetectLoopPass = True
        break
    else:
        #Just displaying the failed detections, don't really need this else
        print("\nUpdate Task Complete Message missed blink check #" + str(BlinkChecknum + 1) + ".\n")

if BlinkDetectLoopPass:
    #Success Handling
else:
    #Error Handling

请发布更好的内容,谢谢!