Reputation: 11
My Python code is working totally fine in my PC, but when I uploaded it in server it shows error. Please see the code its easy to read
from pyvirtualdisplay import Display from selenium import webdriver import time
with Display():
browser = webdriver.Firefox()
try:
browser.set_window_size(1080,800)
browser.get('https://www.instagram.com/accounts/login')
print (browser.title)
time.sleep(5)
# i used the screenshot to cheek the problem , but the screenshot is totally
# blank (just a whit screen )
browser.save_screenshot("screenshot.png")
print("clicked..!")
browser.find_element_by_name("username").send_keys('*****')
browser.find_element_by_name("password").send_keys('*****')
finally:
browser.quit()
Login • Instagram clicked..!
Traceback (most recent call last):
File "/home/Sourabh58/bot1.py", line 25, in <module>
browser.find_element_by_name("username").send_keys('be_fully_motivated')
File "/usr/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 365, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "/usr/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 752, in find_element
'value': value})['value']
File "/usr/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"name","selector":"username"}
Upvotes: 0
Views: 101
Reputation: 168002
Using time.sleep() is a some form of an anti-pattern as your test will wait for 5 seconds even if the element appears in DOM faster.
Consider refactoring your code to use Explicit Waits instead, this way WebDriver will poll the DOM for the element presence (or absence) and continue immediately after the element is found.
Suggested code change:
driver.get('https://www.instagram.com/accounts/login')
username = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.NAME, 'username')))
username.send_keys("****")
password = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.NAME, 'password')))
password.send_keys("****")
this 10
stanza stands for 10 seconds of maximum wait time, you can increase/decrease it according to your test scenario, network bandwidth, application response time, etc.
More information: How to use Selenium to test web applications using AJAX technology
Upvotes: 1