[FIXED] Klicken Sie mit Selen auf die Schaltfläche Anmelden

Problem

In einem Web-Scraping-Projekt wollte ich einige Daten von https://www.businesstimes.com.sg sammeln und mich dort anmelden. Ich konnte nicht herausfinden, wie ich mit Selenium auf die Anmeldeschaltfläche (siehe Bild unten) klicke.

Geben Sie hier die Bildbeschreibung ein

Ich habe auch mit den Methoden CSS_Selector, Class_Name, By.ID versucht, abgesehen von dieser XPATH-Methode, um die Schaltfläche auszuwählen, aber ich hatte keinen Erfolg.

Hier ist mein Code,

def login_in(login_url):

    options = webdriver.ChromeOptions()
    
    lists = ['disable-popup-blocking']

    caps = DesiredCapabilities().CHROME
    caps["pageLoadStrategy"] = "normal"

    options.add_argument("--window-size=1920,1080")
    options.add_argument("--disable-extensions")
    options.add_argument("--disable-notifications")
    options.add_argument("--disable-Advertisement")
    options.add_argument("--disable-popup-blocking")

    
    username = 'insert_username'
    password = 'insert_password'
    
    
    driver = webdriver.Chrome(executable_path= r"E:\chromedriver\chromedriver.exe", options=options) #add your chrome path
    
    driver.get(login_url)
    button = driver.find_element(By.XPATH, '//*[@id="sph_login"]')
    driver.execute_script("arguments[0].click();", button)
    time.sleep(3)
    driver.find_element(By.ID, "IDToken1").send_keys(username)  # input user name
    time.sleep(5)
    driver.find_element(By.ID, "IDToken2").send_keys(password)  # input password
    time.sleep(2)
    loginbutton = driver.find_element(By.ID, "btnLogin")
    driver.execute_script("arguments[0].click();", loginbutton)
    
    return driver

login_in('https://www.businesstimes.com.sg/')

Bitte helfen Sie mir dabei. Danke schön!

Lösung

Sie müssen nicht auf die Schaltfläche klicken. Die Schaltfläche hat ein Onclick-Attribut, sodass Sie dies einfach tun können:
driver.execute_script("_mySPHObj.openLogin()")
und das Anmelde-Popup wird geöffnet.


Beantwortet von –
MrTilllus


Antwort geprüft von –
Terry (FixError Volunteer)

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like

[FIXED] Ich kann keinen Text eines Elements abrufen

Ausgabe ich gehe auf diese website: https://www.drk-kv-calw.de/kurse/erste-hilfe-eh/rotkreuzkurs-erste-hilfe.html driver.get("https://www.drk-kv-calw.de/kurse/erste-hilfe-eh/rotkreuzkurs-erste-hilfe.html"); WebElement bir=driver.findElement(By.xpath("(//a[text()='Link zur Karte'])[4]")); WebElement iki=driver.findElement(By.xpath("(//a[@title='bei Kurs anmelden'])[4]")); WebElement deneme=driver.findElement(RelativeLocator.with(By.tagName("br")).below(bir).toLeftOf(iki));…