How to use find_element_by_id() in python

独自空忆成欢 提交于 2020-06-26 06:51:38

问题


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 not Username:



回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!