1

I'm trying to find an element on the page, but it doesn't work out what could be the reason?

package main

import (
    "github.com/tebeka/selenium"
    "log"
    "time"
)

func main() {
    caps := selenium.Capabilities{
        "browserName": "chrome",
    }

    wd, err := selenium.NewRemote(caps, "http://localhost:4444/wd/hub")
    if err != nil{
        log.Fatal(err)
    }
    defer func() {
        if err := wd.Quit(); err != nil {
            log.Fatal(err)
        }
    }()

    if err := wd.SetPageLoadTimeout(time.Second * 10); err != nil {
        log.Fatal(err)
    }
    if err = wd.SetImplicitWaitTimeout(time.Second * 10); err != nil {
        log.Fatal(err)
    }
    
    if err = wd.Get("https://www.facebook.com/ads/library/?active_status=all&ad_type=political_and_issue_ads&country=ALL&sort_data[direction]=desc&sort_data[mode]=relevancy_monthly_grouped"); err != nil {
        log.Fatal(err)
    }
    
    title, err := wd.Title()
    if err != nil {
        log.Fatalln(err)
    }
    log.Println(title)
    
    elem, err := wd.FindElement(selenium.ByXPATH, "//*[@id=\"content\"]/div/div/div[3]/div/div[4]/div")
    if err != nil {
        log.Fatal(err)
    }
    log.Println(elem.Text())
}

gives an error message:

 no such element: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="content"]/div/div/div[3]/div/div[4]/div"}

The hub was raised in docker. Image https://hub.docker.com/r/selenium/standalone-chrome

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Shytik
  • 11
  • 2

1 Answers1

0

Just try to not use XPATHs with Selenium if you can. Search for it using .ByTag and look for a unique property of your element to find it. Never rely on XPATHs, classes or even IDs for famous websites since they often change.

Check my answer here: How to click on the Ask to join button within https://meet.google.com using Selenium and Python?