0

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/fxdriver@googlecode.com/components/driver-component.js:11885)  
at FirefoxDriver.prototype.findElement (file:///tmp/tmpG1qHlX/extensions/fxdriver@googlecode.com/components/driver-component.js:11894)      
at DelayedCommand.prototype.executeInternal_/k(file:///tmp/tmpG1qHlX/extensions/fxdriver@googlecode.com/components/command-processor.js:13395)   
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpG1qHlX/extensions/fxdriver@googlecode.com/components/command-processor.js:13400)    
at DelayedCommand.prototype.execute/< (file:///tmp/tmpG1qHlX/extensions/fxdriver@googlecode.com/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!

AMC
  • 2,642
  • 7
  • 13
  • 35
  • Does this answer your question? [Selenium - wait until element is present, visible and interactable](https://stackoverflow.com/questions/59130200/selenium-wait-until-element-is-present-visible-and-interactable) – AMC Aug 12 '20 at 00:15

1 Answers1

1

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()
Swaroop Humane
  • 1,770
  • 1
  • 7
  • 17
  • Hey i tried your approach and im getting an error. Traceback (most recent call last): File "test.py", line 24, in G_Search = wait.until(EC.element_to_be_clickable((By.XPATH,"//input[@name='q']"))) File "/usr/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) – Alex Mathew Aug 11 '20 at 19:02