简体   繁体   中英

Can't get selenium to find element and select it

I'm trying to automate login on a few quora accounts to check their revenue.

I'm getting stuck at the first part: the login screen.

I have tried using css_selector, xpath, and name. My problem is the way that the quora login page is set up, the xpath is NEW for every new instance of it, so there is no way to select the element by this.

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1200x600')

driver = webdriver.Chrome(options=options)

driver.get('https://quora.com')

driver.implicitly_wait(3)

email = driver.find_element_by_css_selector('input[type=password]')
password = driver.find_element_by_css_selector('input[type=password]')
login = driver.find_element_by_css_selector('input[value="Login"]')

email.send_keys('')
password.send_keys('')

driver.get_screenshot_as_file('main-page.png')

is what i have so far.

Can anyone look at www.quora.com and figure out which element I should be targeting in order to fill out the form and submit it? Because I've tried for over an hour at this point with no success because nothing works.

Your problem is in wrong selectors

  1. You have input[type=password] selector 2 times which is not correct as you should have different selectors for email and password inputs
  2. Your selector matches 2 elements as it's evidenced by Chrome Developer Tools

    在此处输入图片说明

    therefore Selenium tries to send keys to 1st one which is not visible/interactable

So make sure to:

  • Use correct selectors
  • Use selectors which identify elements in clear and unique manner, this way you will have confidence that you're working with the element you're expecting, ie stick to Placeholder attribute

     email = driver.find_element_by_css_selector('input[Placeholder=Email]') password = driver.find_element_by_css_selector('input[Placeholder=Password]') 
  • During test development it's better to use "normal" browser so you could observe what's going on as the test runs

  • You can also consider sticking to Page Object Model pattern , this way you will be able to quickly update element locators while the logic remains the same.

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