开发者

Clicking to the Login button using selenium

in a web scraping project I wanted to collect some data from https://www.businesstimes.com.sg and wanted to login into this. I couldn't figure out how to click the login button (check the image below) using Selenium.

Clicking to the Login button using selenium

I tried with the CSS_Selector, Class_Name, By.ID methods too, apart from this XPATH method to select the button, but I didn't get success.

Here is my 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,10开发者_如何学运维80")
    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/')

Please help me with this. Thank you!


you do not need to click the button. The button has an onclick attribut so you can just do this: driver.execute_script("_mySPHObj.openLogin()") and the login popup will open.


There are more than one matches for your XPath, that's why the link is not getting clicked, try the below XPath:

driver.find_element(By.XPATH, ".//*[@class='user-signup-section w-100 d-block']//a[text()='LOG IN']").click()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜