Selenium 'send_keys' does not work for YouTube - python

I'm new to Selenium and I'm trying to create my first automatization test where OS is gonna open Chrome browser, opens YouTube and enteres a word in the search bar.
Well, browser opens, YouTube opens, but OS doesn't enter any word.
This is my code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//*[#id="search"]')
searchbox.click()
searchbox.send_keys('Selenium')

Seems like there could be 2 element in the page with the same id as search, we need to get the input element having id search.
The below code, with the changes should work fine now.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//input[#id="search"]')
searchbox.click()
searchbox.send_keys('Selenium')

Related

Using Selenium with Python not working in google chrome

i have code it opens website in chrome for this code but it does not go further
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("C:\Drivers\chromedriver_win32 (2)\chromedriver.exe")
driver.get('https://opensource-demo.orangehrmlive.com/web/index.php/auth/login')
driver.find_element("username").send_keys("Admin")
driver.find_element("password").send_keys("admin123")
driver.close()
Result this one
i need answer how to solve this problem
The way you are initializing your webdriver and selecting the input elements seem to be the problem.
Try doing it this way instead.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://opensource-demo.orangehrmlive.com/web/index.php/auth/login')
time.sleep(3)
driver.find_element(By.XPATH, "//input[#name='username']").send_keys("Admin")
driver.find_element(By.XPATH, "//input[#name='password']").send_keys("admin123")
time.sleep(3)
driver.close()
You may also check out selenium's quick start guide here

How do I click a button on cookies pop up using Selenium?

Hi I want to click 'Save Services' using Selenium on this website to make the pop up disappear: https://www.hugoboss.com/uk/home. However I receive a timeout exception.
import numpy as np
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
import time
driverfile = r'C:\Users\Main\Documents\Work\Projects\extra\chromedriver'
driver = webdriver.Chrome(executable_path=driverfile)
driver.get("https://www.hugoboss.com/uk/men/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(text(),'SAVE SERVICES')]"))).click()
Further information: When I try to find the x_path of the button using class: //button[#data-testid='uc-save-button'] on the inspect element finder, It returns 0 results as if it does not exist?
I ran len(driver.window_handles) 10 seconds after the webpage was loaded and which returned 1, meaning selenium could see one window open only.
Your element is in a shadow root. Find your element in devtools, scroll up and you'll see this in the DOM:
To get into the shadowroot, an easy way is the get it's parent item then use JS to get the object.
Within that returned object you can find your button:
edit:: Updated the code from the original answer. This runs for me:
driver = webdriver.Chrome()
driver.implicitly_wait(10)
url = "https://www.hugoboss.com/uk/home"
driver.get(url)
driver.implicitly_wait(10)
shadowRoot = driver.find_element(By.XPATH,"//div[#id='usercentrics-root']").shadow_root
shadowRoot.find_element(By.CSS_SELECTOR, "button[data-testid='uc-save-button']").click()
#########
Update - a demo of the code working:
pip list tells me I'm using:
selenium 4.1.3

Simulate "ENTER" Selenium

Hello ^^ I am currently coding on Selenium (python webdriver) and I would like to know the command to simulate the "Enter" touch in the script in order to validate a step. How can I do ?
Thank you ^^
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
# Navigate to url
driver.get("http://www.google.com")
# Enter "webdriver" text and perform "ENTER" keyboard action
driver.find_element(By.NAME, "q").send_keys("webdriver" + Keys.ENTER)
This can be found on their docs page. Basically you want to import Keys from selenium.webdriver.common.keys and then use element.send_keys(Keys.Enter) as needed.
source

I cannot locate a download link by using Selenium in the drop down menu

I am trying to learn selenium and i am trying to download an excel file from a drop down menu.
Firstly, i click to button in order to open it and afterwards when i do inspect i am able to get to this part.
I am trying to click this part and download the file.
<span _ngcontent-bke-c150="" class="left-text">Excel</span>
Here is the link to the website: https://survey123.arcgis.com/
I don't think sharing my code would help because i am already stuck at that very specific part. I was able to login to the website through selenium and enter id and password but failed at downloading the excel file.
But here it is
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
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
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome()
driver.get("https://survey123.arcgis.com/")
inputElement_user = driver.find_element_by_id("user_username")
inputElement_user.send_keys("myusername")
inputElement_password = driver.find_element_by_id("user_password")
inputElement_password.send_keys("mypassword")
giris_button = driver.find_element_by_id("signIn")
actions = ActionChains(driver)
actions.click(giris_button)
actions.perform()
continue_link = driver.find_element_by_partial_link_text('Rapor')
This might not exactly be the answer but I had also stuck on this point some time ago. I tried this <a href="file/path/or/link/here" download> and it worked.
the download property makes files downloadable, if the href is set properly. hope this works in your problem as well.

Unable to submit keys using selenium with python

Following is the code which im trying to run
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
#Create a new firefox session
browser=webdriver.Firefox()
browser.maximize_window()
#navigate to app's homepage
browser.get('http://demo.magentocommerce.com/')
#get searchbox and clear and enter details.
browser.find_element_by_css_selector("a[href='/search']").click()
search=browser.find_element_by_class_name('search-input')
search.click()
time.sleep(5)
search.click()
search.send_keys('phones'+Keys.RETURN)
However, im unable to submit the phones using send_keys.
Am i going wrong somewhere?
Secondly is it possible to always use x-path to locate an element and not rely on id/class/css-selections etc ?
The input element you are interested in has the search_query class name. To make it work without using hardcoded time.sleep() delays, use an Explicit Wait and wait for the search input element to be visible before sending keys to it. Working code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Firefox()
browser.maximize_window()
wait = WebDriverWait(browser, 10)
browser.get('http://demo.magentocommerce.com/')
browser.find_element_by_css_selector("a[href='/search']").click()
search = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "search-query")))
search.send_keys("phones" + Keys.RETURN)

Categories