简体   繁体   中英

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

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:

饼干

以下 css Selector 应唯一标识您要查找的按钮:

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

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 , ie: $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")]")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM