Search code examples
pythonselenium-webdriver

Python and Selenium script and setting focus not working


I have some code I open up a web page and then I try to set focus on a test box so a user can start typing, however when the page opens and the cursor is flashing on the correct text box if the user starts typing the URL bar is where the typing will occur how do I get it to show in the text box?

    # import the required libraries
import undetected_chromedriver as uc

 
# define Chrome options
options = uc.ChromeOptions()

# set headless to False to run in non-headless mode
options.headless = False

# set up uc.Chrome(use_subprocess=True, options=options)

from arcgis.gis import GIS
from arcgis.geometry import Point, Polyline, Polygon
import datetime
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager 
import time
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


#driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver = uc.Chrome(use_subprocess=True, options=options)
driver.get("https://library.usask.ca/#gsc.tab=0")



q_field = driver.find_element("id", "primoQueryTemp")
q_field.send_keys("_")
q_field.click()

time.sleep(566)

what I tried is in there find_element send_keys and even .click() but it is still defaulting to URL box


Solution

  • I'm not sure what's causing your issue but I'm assuming it's some of the chrome options and/or the undetected_chromedriver. It could also be that you have a mismatched driver. The code below works and will also handle downloading the current chromedriver for you.

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    
    driver = webdriver.Chrome()
    driver.get("https://library.usask.ca/#gsc.tab=0")
    
    wait = WebDriverWait(driver, 10)
    
    driver.find_element(By.ID, "primoQueryTemp").send_keys("elon musk")
    driver.find_element(By.CSS_SELECTOR, "button[title='Search']").click()
    
    links = wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "h3 > a")))
    for link in links:
        print(link.text)
    

    and it outputs

    Elon Musk
    Elon Musk : Tesla, SpaceX, and the quest for a fantastic future 
    TEDTalks, Elon Musk—The mind behind Tesla, SpaceX, SolarCity ...
    Elon Musk and Tesla : an electrifying love affair
    The last days of twitter? : Elon Musk's calamitous takeover
    Elon Musk's Twitter Takeover
    Is Elon Musk Killing Twitter?, A Debate 
    Elon Musk and Twitter : a saga of tweets
    Elon Musk and Twitter : a hard pill to swallow
    Once a physicist: Elon Musk