Reputation: 317
Currently have a gmail account with no emails in the inbox. I want to be able to display a popup with the current number of emails. If there are no emails, then no message is displayed.
So I'm choosing to use Explicit Waits & Xpath to do this. It's to my understanding that this allows me define a time period (say 10 s) and then each 500 ms, the code would query and check to see if the element contained the right text, as per specified by the second argument to the function below.
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_present_in_element((By.XPATH, "//*[@id=':3s']/div/div[1]/span/a/"), 'Inbox (1)'))
The xpath location is the xpath of the Inbox button. It has a value that changes. (you can verify this in your own browser if you like)
I actually just ran it for the first time (ran it for the first time, after I posted, don't ask why) and got this error code:
Traceback (most recent call last):
File "C:/Users/SG/Desktop/Report/Web_2.py", line 47, in <module>
wait.until(EC.element_to_be_present_in_element((By.XPATH, "//*[@id=':3s']/div/div[1]/span/a/"), 'Inbox (1)'))
AttributeError: 'module' object has no attribute 'element_to_be_present_in_element'
After implementing:
wait.until(EC.presence_of_element_located((By.XPATH, "//*[@id=':3s']/div/div[1]/span/a[text()[contains(.,'Inbox (1)')]]")))
I'm still getting this error at compile:
Traceback (most recent call last):
File "C:/Users/singhgurp/Desktop/Report/Web_2.py", line 51, in <module>
wait.until(EC.presence_of_element_located((By.XPATH, "//*[@id=':3s']/div/div[1]/span/a[text()[contains(.,'Inbox (1)')]]")))
File "C:\Python34\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///C:/Users/SINGHG~1/AppData/Local/Temp/tmpq64vsuyi/extensions/[email protected]/components/driver-component.js:10770)
at FirefoxDriver.prototype.findElement (file:///C:/Users/SINGHG~1/AppData/Local/Temp/tmpq64vsuyi/extensions/[email protected]/components/driver-component.js:10779)
at DelayedCommand.prototype.executeInternal_/h (file:///C:/Users/SINGHG~1/AppData/Local/Temp/tmpq64vsuyi/extensions/[email protected]/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///C:/Users/SINGHG~1/AppData/Local/Temp/tmpq64vsuyi/extensions/[email protected]/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///C:/Users/SINGHG~1/AppData/Local/Temp/tmpq64vsuyi/extensions/[email protected]/components/command-processor.js:12608)
The page has 1 email so it should work, not sure why it's not.
Upvotes: 0
Views: 889
Reputation: 1577
You should use:
wait.until(EC.presence_of_element_located((By.XPATH, "//*[@id=':3s']/div/div[1]/span/a"))
In selenium-python doc, there is no method element_to_be_present_in_element
to check if there is any element as expected in the page, however doc refs the method presence_of_element_located
.
Doc: http://selenium-python.readthedocs.io/waits.html
Upvotes: 1