Home > Mobile >  selenium webdriver doesn't hold opened python
selenium webdriver doesn't hold opened python

Time:08-28

i want to creat a automation go to keywordtool.io and search for an topic and collect the keyword from it

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

Path = "C:\Program Files\chromedriver.exe"
##browser=webdriver.Chrome(ChromeDriverManager().install())
browser= webdriver.Chrome(Path)
browser.get('https://keywordtool.io')

search = browser.find_element(By.ID, 'search-form-google-keyword-md')
search.send_keys("funny shirt")
search.send_keys(Keys.RETURN) # hit return after you enter search text
wait(10)  ```

what is the probleme here

CodePudding user response:

There are several issues here:

  1. You need to wait for the page to be loaded before applying this line
search = browser.find_element(By.ID, 'search-form-google-keyword-md')

The most recommended way to do that is to use WebDriverWait expected_conditions explicit waits.
2) with your current code this line wait(10) will cause exception since wait object still not defined.
3) if you want the browser to stay opened you can use the 'detach' option when starting chromedriver as following:

import time
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

path = "C:\Program Files\chromedriver.exe"
browser= webdriver.Chrome(executable_path = path, options = chrome_options)
wait = WebDriverWait(browser, 20)

browser.get('https://keywordtool.io')

search = wait.until(EC.visibility_of_element_located((By.ID, "search-form-google-keyword-md")))

search.send_keys("funny shirt")
search.send_keys(Keys.RETURN) # hit return after you enter search text

CodePudding user response:

How I understood your Question you want that the browser keeps open 10 seconds after the enter. When that is what you want you have to use time.sleep(10) instead of wait(10).

  • Related