switch_to_alert.text()无效

时间:2015-11-01 20:29:35

标签: python selenium

我在python中使用selenium webdriver来抓取弹出窗口的内容。我使用driver.switch_to_alert.text()来检索内容。

我在拨打driver.switch_to_alert时收到提醒对象,但我无法使用对象的acccept()dismiss()和文字功能。

如果我呼叫警报,我会得到该物体 <selenium.webdriver.common.alert.Alert at 0x438fbe0>

但是如果调用alert.text我会收到以下错误

NoAlertPresentException                   
Traceback (most recent call last)
<ipython-input-162-7b8c4cd45721> in <module>()
----> 1 alert.text
C:\Users\\Anaconda\lib\site-packages\selenium\webdriver\common\alert.pyc in text(self)
     63         Gets the text of the Alert.
     64         """
---> 65         return self.driver.execute(Command.GET_ALERT_TEXT)["value"]
     66 
     67     def dismiss(self):

我尝试按sleep(5)延迟执行,但没有任何作用。

1 个答案:

答案 0 :(得分:2)

Wait for the alert to be present然后切换到它:

jsfiles=()
cssfiles=()
htmlfiles=()
imagesfiles=()

# Loop over the output from `git diff --name-only -z` to operate by file.
while IFS= read -r -d '' file || [ -n "$file" ]; do
    # gzip the '.js', '.css' and '.html' in place while looping
    # also set up the arrays as valid arguments (hopefully) for the 'aws s3 sync' commands later.
    case "$file" in
        *.js)
            gzip "$file" && mv "$file"{.gz,} && jsfiles+=(--include "$file")
            ;;
        *.css)
            gzip "$file" && mv "$file"{.gz,} && cssfiles+=(--include "$file")
            ;;
        *.html)
            gzip "$file" && mv "$file"{.gz,} && htmlfiles+=(--include "$file")
            ;;
        images/*)
            imagesfiles+=("$file")
            ;;
    esac
done <(git diff --name-only -z "$revision")

aws s3 sync . s3://test --content-type "JS" --cache-control "max-age=31536000" --exclude "*" "${jsfiles[@]}"
aws s3 sync . s3://test --content-type "CSS" --cache-control "max-age=31536000" --exclude "*" "${cssfiles[@]}"
aws s3 sync . s3://test --content-type "text/html" --cache-control "max-age=31536000" --exclude "*" "${htmlfiles[@]}"

cp "${imagesfiles[@]}" "$some_other_dir/"