Trying to validate the complete loading of a page in selenium python through a VM

343 views Asked by At

I am trying to run a simple selenium automation script (which just loads up google ,searches for youtube and hits the search button) through a virtual machine.So when i set up the options/parameters as shown in the code below (even setting headless mode on), this is what i have: #Beginning of code

from pyvirtualdisplay import Display   
from selenium import webdriver  
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary  
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities.  
from selenium.webdriver.firefox.options import Options 
from selenium.webdriver.support.ui import WebDriverWait  
from selenium.webdriver.common.keys import Keys

display =Display(visible=0, size=(1024,768))  
display.start()  
options = Options()  
options.headless = False  
caps = DesiredCapabilities.FIREFOX  
caps['marionette'] = False  
binary = FirefoxBinary('/home/a-user/Al/firefox/firefox')  
browser = webdriver.Firefox(firefox_binary= binary, options = options, capabilities= caps,executable_path=r"/home/a-user/Al/firefox/geckodriver")  
browser.get('https://www.google.com')  
browser.find_element_by_name('q').send_keys("Youtube")
browser.find_element_by_xpath('//[@id="tsf"]/div[2]/div[1]/div[3]/center/input[1]').send_keys(Keys.ENTER)  
print("Headless Firefox Initialized")  
browser.close()  
display.stop()

#End of code

But when i try to run it, it shows:
---Beginning of error message ----

Traceback (most recent call last):   
 File "test.py", line 17, in <module>
   browser.find_element_by_name('q').send_keys("Youtube")  
 
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 496, in find_element_by_name
   return self.find_element(by=By.NAME, value=name)    
 File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
   'value': value})['value']    
 File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
   self.error_handler.check_response(response)   
 File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
   raise exception_class(message, screen, stacktrace)    
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"name","selector":"q"}
Stacktrace:      
at FirefoxDriver.prototype.findElementInternal_(file:///tmp/tmpG1qHlX/extensions/[email protected]/components/driver-component.js:11885)  
at FirefoxDriver.prototype.findElement (file:///tmp/tmpG1qHlX/extensions/[email protected]/components/driver-component.js:11894)      
at DelayedCommand.prototype.executeInternal_/k(file:///tmp/tmpG1qHlX/extensions/[email protected]/components/command-processor.js:13395)   
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpG1qHlX/extensions/[email protected]/components/command-processor.js:13400)    
at DelayedCommand.prototype.execute/< (file:///tmp/tmpG1qHlX/extensions/[email protected]/components/command-processor.js:13342)

I was wondering if theres any way I can make sure the page gets loaded properly, then continue with the automated interaction with the elements.Thanks in advance for the help!

1

There are 1 answers

1
Swaroop Humane On

You are getting this error selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"name","selector":"q"} because your driver is not able to find the element with name = 'q' might be because the page is not loaded fully. Refer the below code and make changes in your code accordingly.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 5)
action = ActionChains(driver)

driver.get("https://www.google.com/")
G_Search = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@name='q']")))
action.move_to_element(G_Search).send_keys("Youtube").send_keys(Keys.ENTER).perform()