• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Prevent pages from popups pages

YasserKhalil

Well-Known Member
Hello everyone
I started to deal with selenium and I have this code
Code:
Sub Test()
    Dim bot As New WebDriver
    Dim r As Long
    Dim c As Long

    bot.Start "chrome", "http://electoralservicessearch.azurewebsites.net"
    bot.Get "/searchbyname.aspx"

    For r = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        With Sheets(1)
            c = 1
            bot.FindElementById("txt_Epicno").SendKeys (CStr(.Cells(r, 1).Value))
            bot.FindElementById("btn_Epic").Click

            If bot.FindElementById("lblmsg").Text Like "Your Record is Not Available*" Then
                .Cells(r, c + 1).Value = "Your Record is Not Available Now. For Further Enquiry Please Call 1950."
            Else
                .Cells(r, c + 1).Value = bot.FindElementById("lbnameen").Text
                .Cells(r, c + 2).Value = bot.FindElementById("lbparten").Text
                .Cells(r, c + 3).Value = bot.FindElementById("lbserialen").Text
                .Cells(r, c + 4).Value = bot.FindElementById("lbpartnameen").Text
            End If
        End With
    Next r
End Sub

Data in column A
BDZ2630614
BKH2950970
MSS3007457
DDX2789360

The problem is that when posting a value using sendkeys I found that there are windows opening in new tabs (advertisements) and that make the code fails
How to prevent those advertisements ..?
 
FYI - I think with exception of @shahin most of us in the forum don't use Selenium. Personally I'm not big fan of it. You may have better luck asking in Selenium forum or in Stackoverflow.
 
The same result .. I can't escape those disturbing popups

I even changed "chrome" to "IE" and the same problem
 
Try this. It should lead you fetch whatever items you wanna parse from that page disabling notifications programmatically. I've passed a hardcoded value to the ".SendKeys()" to let you know that it works.

Code:
Sub Test_sth()
    Dim driver As New ChromeDriver, post As Object
   
    With driver
        .AddArgument "--disable-notifications"
        .get "http://electoralservicessearch.azurewebsites.net/searchbyname.aspx"
        .FindElementById("txt_Epicno").SendKeys ("MSS3007457")
        .FindElementById("btn_Epic").Click
    End With
   
    Set post = driver.FindElementById("lbacnameen")
    [A1] = post.Text
   
    driver.Quit
End Sub
 
Thanks a lot for great help Shahin
The problem is in looping .. I got the same result for all the items in column A
This is my try till now
Code:
Sub Scrape_Voters_ElectoralServicesSearch_Using_Selenium_Loops()
    Dim bot        As New WebDriver
    Dim ele        As Object
    Dim esa        As Object
    Dim r          As Long
    Dim c          As Long

    bot.AddArgument "--disable-notifications"
    bot.Start "chrome", "http://electoralservicessearch.azurewebsites.net"
    bot.Get "/searchbyname.aspx"

    For r = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        With Sheets(1)
            c = 1
            Set esa = bot.FindElementById("searchbyname1", timeout:=0, Raise:=False)
            Application.Wait (10000)

            If esa Is Nothing Then
                bot.FindElementById("txt_Epicno").SendKeys (CStr(.Cells(r, 1).Value))
                bot.FindElementById("btn_Epic").Click
            Else
                bot.FindElementById("searchbyname1").Click
            End If

            Set ele = bot.FindElementById("lblmsg", timeout:=0, Raise:=False)

            If Not ele Is Nothing Then
                .Cells(r, c + 1).Value = "Your Record is Not Available Now. For Further Enquiry Please Call 1950."
            Else
                .Cells(r, c + 1).Value = bot.FindElementById("lbnameen").Text
                .Cells(r, c + 2).Value = bot.FindElementById("lbparten").Text
                .Cells(r, c + 3).Value = bot.FindElementById("lbserialen").Text
                .Cells(r, c + 4).Value = bot.FindElementById("lbpartnameen").Text
            End If

            Set esa = Nothing: Set ele = Nothing
        End With
    Next r

    bot.Quit
End Sub
 
Last edited:
disable notification doesn't prevent pages to be opened in new tabs ..
I need a way to prevent new tabs from being created in that case
 
What happens when you do something like below. Your logic was wrong. However, It's a quick demo so I didn't try to avoid "On error resume next".

Code:
Sub Test_sth()
    Dim driver As New ChromeDriver, post As Object
    Dim lrow As Long, cel As Range

    lrow = Range("A" & Rows.Count).End(xlUp).Row
   
    For Each cel In Range("A1:A" & lrow)
        With driver
            .AddArgument "--disable-notifications"
            .get "http://electoralservicessearch.azurewebsites.net/searchbyname.aspx"
            .FindElementById("txt_Epicno").SendKeys (cel)
            .FindElementById("btn_Epic").Click
        End With
        On Error Resume Next
        Set post = driver.FindElementById("lbacnameen")
        cel(1, 2) = post.Text
        On Error GoTo 0
    Next cel
End Sub

Search keywords are in Range("A1:A4"):
BDZ2630614
BKH2950970
MSS3007457
DDX2789360
 
Last edited:
I really liked that. This is a successful point till now
How to put "Not Available" if not found .. I know you hate "On Error Resume Next" and me too
So how to handle this error
I tried to make skip points "On Error GoTo Nxt .. but I encountered errors later for not finding other records
 
I really get stuck while dealing with that vicious line. However, few legends are here in this forum and I always seek help to them if I'm unable to bypass errors. You know the list of those legends.
 
Thanks a lot anyway. I have reached a very good point in this issue
Thank you very very much for awesome help
You are really brilliant
 
Back
Top