Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 698
gosetty chakravarthy Avatar asked Dec 01 '22 08:12

gosetty chakravarthy


2 Answers

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:
like image 84
Isaac Avatar answered Dec 05 '22 18:12

Isaac


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="...")
like image 26
Ruggiero Spearman Avatar answered Dec 05 '22 18:12

Ruggiero Spearman