gosetty chakravarthy
gosetty chakravarthy

Reputation: 79

How to use find_element_by_id() in python

I am new to selenium. I am trying to set my username in in gmail browser by using python2.6.

import unittest
from selenium import webdriver;
from selenium import WebElement;
driver = webdriver.Ie();
driver.get("http://www.gmail.com");

WebElement uname = driver.find_element(by=By,id("Username:"));

uname.sendKeys("chakry.gosetty");

With the above code I am getting

WebElement uname= driver.find_element(by=By,id("Username:"));
SyntaxError : Invalid syntax.

Please help me.

Rgds, G.Chakravarthy

Upvotes: 7

Views: 50488

Answers (3)

Akash Bhatia
Akash Bhatia

Reputation: 44

Isaac's answer is correct with a small modification. The email field on gmail is an input type field that does not have a reliable id. I would go with below syntax to identify that field.

driver.find_element_by_css_selector('input[type='email']')

Since your question was about how to find an element using ID the answer is

driver.find_element_by_id("idLocatorValue")

Upvotes: 0

Isaac
Isaac

Reputation: 9652

How about...

import unittest
from selenium import webdriver

driver = webdriver.Ie()
driver.get('http://www.gmail.com')

driver.find_element_by_id('Email').send_keys('chakry.gosetty')
  • you don't need semicolons as line terminators in python :)
  • also, the username field ID is actually Email not Username:

Upvotes: 16

Ruggiero Spearman
Ruggiero Spearman

Reputation: 6905

A guess: Apart from the other obvious syntax errors, you don't import By. How about this:

from selenium.webdriver.common.by import By
...
uname = driver.find_element(By.ID,value="...")

Upvotes: 7

Related Questions