Selenium VBA在步骤F8上运行正常,但在完整F5下运行时跳过一行

时间:2018-11-13 20:10:14

标签: excel vba selenium selenium-webdriver selenium-chromedriver

我在整个互联网和这个论坛中进行了搜索,找不到答案。以下代码用于将文件上传到wordpress网站。当我使用F8单步执行时,它可以正常工作。但是,当运行完整代码时,使用XPath ("//input[starts-with(id, 'htm15_')]").SendKeys (ws.Cells(1.4))上传之后,宏似乎跳过了下一行,即单击按钮(插入文件URL),浏览器停留在该上传页面上,并且不会加载出现“发布”按钮的下一页。此时,出现错误并指出找不到发布元素。

我尝试了不同的方法,添加了全局ImplicitWait,为上述代码行添加了超时,但无济于事。似乎在上传文件之后,宏没有单击按钮(跳过该行代码),因此无法在下一行中找到“发布”按钮。代码的问题行用2个星号表示。感谢任何帮助。

The Error Message

Public Sub UploadPost()

 Dim ws As Worksheet
 Dim filename As String
 Dim wb As Workbook
 Dim D As WebDriver
 Dim Path As String
 Dim i As Integer

    Set ws = ThisWorkbook.Worksheets("Data")
    Set D = New ChromeDriver
    Const URL = "https://www.myweb.com/login/"

'To login to website

    With D
        .Timeouts.ImplicitWait = 10000    'global implicit wait 5 seconds than default 3 seconds
        .Start "Chrome"
        .Window.Maximize
        .Get URL
        .FindElementById("user_login").SendKeys("id").Click
        .FindElementById("user_pass").SendKeys("pw").Click
        .FindElementByName("submit").Click

    End With

'Upload daily file to Download Monitor and get file ID

        ws.Range("H2:H9").Clear
        D.FindElementById("menu-posts-dlm_download").Click          'Click Download on left menu

  For i = 2 To 9

    With D

        .FindElementByClass("page-title-action").Click                                          'Click Add New
        .FindElementById("title").SendKeys(ws.Cells(i, 2)).Click                                'Type title copy from spreadsheet
        .FindElementById("_members_only").Click                                                 'Click members only category
        .FindElementById(ws.Cells(i, 3)).Click                                                  '17-china, 21-HK, 25-NAS, 10-NYSE
        .FindElementByLinkText("Add file").Click                                                'Add File
        .FindElementByXPath("//input[starts-with(@id,'dp')]").Clear                             'Select the correct date
        .FindElementByXPath("//input[starts-with(@id,'dp')]").SendKeys (ws.Cells(i, 9))
        .FindElementByLinkText("Upload file").Click
        .FindElementByLinkText("Upload Files").Click
        .FindElementByXPath("//input[starts-with(@id,'html5_')]").SendKeys (ws.Cells(i, 4))      'upload file using path from spreadsheet
        **.FindElementByXPath("//*[@id='__wp-uploader-id-0']/div[5]/div/div[2]/button").Click      'Click save Insert file URL
        .FindElementById("publish").Click**
        ws.Cells(i, 8) = D.FindElementByXPath("//*[@id='dlm-info-url']").Value


    End With

   Next i

0 个答案:

没有答案