问题
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
回答1:
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
notUsername:
回答2:
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="...")
回答3:
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")
来源:https://stackoverflow.com/questions/6481973/how-to-use-find-element-by-id-in-python