0

I executed:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import time

print("Opening...")
driver = webdriver.Chrome()
driver.get('https://google.com')
driver.execute_script("window.open('https://www.yahoo.com');") #New tab
search = driver.find_element_by_xpath("//input[@type='text' and @id='uh-search-box']")
search.send_keys('Hello')

Gave me Error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Una ble to locate element: {"method":"xpath","selector":"//input[@type='text' and @i d='uh-search-box']"}

This error was because it did not complete loading in chrome and executed further codes...

How to get rid of that?

How to wait for the load and execute the further script...(Just like it did for first tab, google.com)?

Grass
  • 185
  • 2
  • 14
  • Can anyone please help me with: 1) [Chrome opens with “Data;” with selenium chromedriver](https://stackoverflow.com/questions/52243080/chrome-opens-with-data-with-selenium-chromedriver) and 2) [Console Log/ cmd.exe not closing in chromedriver](https://stackoverflow.com/questions/52236941/console-log-cmd-exe-not-closing-in-chromedriver) – Grass Sep 10 '18 at 17:49

1 Answers1

1

Try to switch to new window as below:

current = driver.current_window_handle
driver.execute_script("window.open('https://www.yahoo.com');") #New tab
new_window = [window for window in driver.window_handles if window != current][0] # Get new tab ID
driver.switch_to.window(new_window) # Switch to new tab
search = driver.find_element_by_xpath("//input[@type='text' and @id='uh-search-box']")
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • I don't know why they downvoted, no coments were placed: https://stackoverflow.com/questions/52223642/how-to-hide-cmd-exe-console-log-of-chromedriver-in-selenium-in-python – Grass Sep 07 '18 at 15:56
  • Have no idea. You can check [this ticket](https://stackoverflow.com/questions/46423361/chrome-devmode-suddenly-turning-on-in-selenium), but it seem that solution doesn't work for latest Chromedriver versions... What about current issue? If my answer solved it don't forget to [accept it](https://stackoverflow.com/help/accepted-answer) – Andersson Sep 07 '18 at 16:40
  • But then my `driver.switch_to.window(driver.window_handles[0]) #First tab` code doesn't work (switching to the previous tab).... – Grass Sep 08 '18 at 04:30
  • Try `driver.switch_to.window(current)` – Andersson Sep 08 '18 at 05:52
  • Ohh Sorry, by-mistake commented wrong, the code in comment worked well for me.. My comment was: Whenever I open chrome from the selenium it also opens one tab ([see image](https://imgur.com/DfVcjya)), why it opens? How to stop that? – Grass Sep 08 '18 at 16:25
  • It's hard to tell why it happens. I should see your code. – Andersson Sep 08 '18 at 16:28
  • You defined your webdriver instance as `driver = webdriver.Chrome(chrome_options=options)`, but I see no `options` definition. You'd better to create new ticket regarding this new issue with *exact* code you use – Andersson Sep 08 '18 at 16:38