Reputation: 111
I have written a script which sends an email using Selenium Webdriver (Python bindings). But sometimes, when I run that script the ID of element gets changed.
Error trace-back:
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium-3.4.3-
py2.7.egg/selenium/webdriver/remote/errorhandler.py", line 194, in
check_response
raise exception_class(message, screen, stacktrace)
elenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element: {"method":"id","selector":":pm"}
And sometimes, if I am using valid XPath, or CSS Selector, then the same error is displayed.
Can anyone suggest the best way to make the script work? Also, why is it that every time the Id of the element gets changed?
My code:
from selenium import webdriver
import time
from time import sleep
driver = webdriver.Chrome()
driver.get("https://accounts.google.com")
driver.implicitly_wait(30)
usr = driver.find_element_by_id("identifierId").clear()
usr.send_keys("[email protected]")
nextb = driver.find_element_by_id("identifierNext")
nextb.click()
pwd = driver.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input')
pwd.send_keys("**Enter your password**")
nextpwd = driver.find_element_by_xpath('//*[@id="passwordNext"]/content/span')
nextpwd.click()
driver.implicitly_wait(30)
driver.find_element_by_xpath("//*[@id=':hs']/div/div").click()
time.sleep(3)
to = driver.find_element_by_id(":nf")
to.send_keys("[email protected]")
sub = driver.find_element_by_id(":my")
sub.send_keys("with attachment")
descp = driver.find_element_by_id(":nx")
descp.send_keys(" Hi! this second one.")
driver.find_element_by_id(":om").click() #attachmment
driver.find_element_by_css_selector("input[type=\"file\"]").send_keys("/home/example.png")
#sead the mail
driver.find_element_by_id(":mo").click()
Upvotes: 1
Views: 2311
Reputation: 1
d.findElement(By.xpath("//*[@id='extension-settings-grid']/div/div[4]/div/div/div[3]/div[1]/div/div/div")).click();
//d.findElement(By.className("dx-item-content dx-toolbar-item-content")).click();
Upvotes: 0
Reputation: 2583
You mean that Id is dynamic? It means that upon each page load element's id changing fully or partly.
In case there's no static part (each time id is 100% random) - you won't be able to use it as locator and should use other attributes/elements to find required one. Like //someParentElement/div[@class='someClass']
- meaning that you can find element's parent element, or use additional attributes like class/value/etc.
In case there's static part
upon each load - you can use css or xPath locator that search for element which contains/starts-with static part
.
Search for element where id contains some part
:
//*[contains(@id,'part')]
- id contains part xpath locator
[id*=part]
- id contains part css locator
Search for element where id starts with some part
:
//*[starts-with(@id,'part')]
- id starts with part xpath locator
[id^=part]
- id starts with part css locator
Upvotes: 4