user10358702
user10358702

Reputation:

Cannot connect to webpage using selenium and Python

I am trying to connect to the particular webpage but it does not click on Login button:

browser.get('https://www.tsago.gr/eshop/account')
print('Browser Opened')
username = browser.find_element_by_id('email')
username.send_keys(email)
password = browser.find_element_by_id('password')

password.send_keys(pwd)
time.sleep(2)
sing_in = browser.find_element_by_xpath('//*[@class="btn btn-primary"]')
sing_in.click()
print('Login Clicked')

I have tried find element by css, by name etc but I dont know why is not clicking on the button in order to login

The html part is the following

<div class="span3 float-right" style="float:right;">                                   
   <button type="submit" class="btn btn-primary">Connect</button>
</div>

The username and password are written and they are correct

Upvotes: 1

Views: 552

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193058

Seems you were pretty close. Perhaps Cookies Message Bar was the obstacle and you can either accept the cookies or scroll the Connect button into view and you can use the following solution:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument("--disable-extensions")
    # options.add_argument('disable-infobars')
    browser = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    browser.get('https://www.tsago.gr/eshop/account')
    print('Browser Opened')
    WebDriverWait(browser, 30).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#email[name='email']"))).send_keys("Nikos")
    browser.find_element_by_css_selector("input#password[name='password']").send_keys("Nikos")
    browser.execute_script("window.scrollBy(0,250)", "");
    browser.find_element_by_css_selector("div.control-group div.span3 button.btn.btn-primary").click()
    print('Login Clicked')
    
  • Console Output:

    Browser Opened
    Login Clicked
    
  • Browser Snapshot:

cookies

Upvotes: 1

itay zohar
itay zohar

Reputation: 187

this is working for me:

driver.find_element_by_css_selector('div.span3 > button[type="submit"]').click()

generally, you should make sure you identify the correct element before coding. to do so - use devTools (f12 on chrome/firefox), and run your query in the 'console' tab

for xpath identification try $x, i.e: $x('//button[@type="submit"]').

for css selector use $$ like so : $$("div.someClass#someID")

you can than hover over the results and the found elements would highlight

edit: i noticed you had trouble with the css option, you can allso try

driver.find_element_by_xpath('//div[contains(@class,"span3")]/button[contains(@class,"btn-primary")]")

Upvotes: 0

C. Peck
C. Peck

Reputation: 3711

The following css Selector should uniquely identify the button you are looking for:

driver.find_element_by_css_selector('div.span3.float-right > button')

Upvotes: 0

Related Questions