I'm trying to send keys to the Youtube search box with Selenium, I've tried search ID and Xpath and consistently get errors. These errors include "Message: element not interactable", where am I going wrong?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://youtube.com")
searchbox = driver.find_element("xpath", '//*[#id="search-input"]')
searchbox.send_keys('Jack West')
There are two problems:
First:
Your xpath gives <div> but you can't send keys to <div>
You have to get <input> which is inside <div>
'//*[#id="search-input"]//input'
And later it needs to click button to start searching.
Second:
When I run code browser displays popup window and asks to accept cookies, etc. And this popup blocks access to search field. It needs to add code which will accept it.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
#from webdriver_manager.firefox import GeckoDriverManager
import time
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
#driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))
driver.get("https://youtube.com")
time.sleep(5)
print('cookies')
cookies = driver.find_elements(By.XPATH, '//div[contains(#class,"eom-button-row")]//a')
cookies[1].click()
time.sleep(5)
print('searchbox')
searchbox = driver.find_element(By.XPATH, '//*[#id="search-input"]//input')
searchbox.send_keys('Jack West')
print('searchbox button')
searchbox_button = driver.find_element(By.XPATH, '//button[#id="search-icon-legacy"]')
searchbox_button.click()
But it could be simpler to use url
https://www.youtube.com/results?search_query=Jack+West
and it doesn't need to click any keys or buttons.
YouTube has API for developers/programmers and this should be preferred method.
Related
I am trying to get the webdriver to click a button on the site random.org The button is a generator that generates a random integer between 1 and 100. It looks like this:
After inspecting the webpage, I found the corresponding element on the webpage looks something like this:
It is inside an iframe and someone suggested that I should first switch over to that iframe to locate the element, so I incorporated that in my code but I am constantly getting NoSuchElementException error. I have attached my code and the error measage below for your reference. I can't understand why it cannot locate the button element despite referencing the ID, which is supposed to unique in the entire document.
The code:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Edge()
driver.get("https://www.random.org/")
driver.implicitly_wait(15)
driver.switch_to.frame(driver.find_element(By.TAG_NAME, "iframe"))
button = driver.find_element(By.CSS_SELECTOR, "input[id='hnbzsqjufzxezy-button']")
button.click()
The error message:
Make sure that there are no more Iframes on the page. If there are a few an not only one do this:
iframes = driver.find_elements(By.CSS, 'iframe')
// try switching to each iframe:
driver.switch_to.frame(iframes[0])
driver.switch_to.frame(iframes[1])
You can't find the button because its name contain random letters. Every time you will refresh the page you can see that the name value will change. So, do this:
button = driver.findElement(By.CSS, 'input[type="button"][value="Generate"]')
button.click()
There are several issues with your code:
First you need to close the cookies banner
The locator of the button is wrong. It's id is dynamic.
You need to use WebDriverWait to wait for elements clickability.
The following code works:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = 'https://www.random.org/'
driver.get(url)
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick*='all']"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME, "iframe")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[id*='button']"))).click()
On the mentioned website, After searching for the token, a slider captcha appears.
An example of the captcha:
I want to bypass the slider captcha. I took reference from the first solution in Unable to let my script slide a button to the right
My approach would be to slowly move until the slider is in the right place and after that, the new page opens.
The website is: https://www.ems.com.cn/english/
My approach:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
from selenium.webdriver import ActionChains
driver = webdriver.Chrome(ChromeDriverManager().install())
actions = ActionChains(driver)
url = 'https://www.ems.com.cn/english/'
driver.get(url)
token = 'CY008445045CN'
token_space = driver.find_element_by_xpath("//input[#class='el-input__inner']")
token_space.send_keys(token)
driver.find_element_by_xpath("//i[#class='el-icon-search']").click()
time.sleep(4)
slider_container = driver.find_element_by_xpath("//div[#class='slide-verify-slider']")
slider = driver.find_element_by_xpath("//div[#class='slide-verify-slider-mask-item']")
# Perform sliding action
actions.move_to_element(slider).click_and_hold().move_by_offset(slider_container.size['width'], 0).release().perform()
find_element_by_xpath
is deprecated, you should use:
find_element(By.XPATH, value="")
Here is updated version of your code:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
actions = ActionChains(driver)
url = 'https://www.ems.com.cn/english/'
driver.get(url)
token = 'CY008445045CN'
token_space = driver.find_element(By.XPATH, value="//input[#class='el-input__inner']")
token_space.send_keys(token)
driver.find_element(By.XPATH, value="//i[#class='el-icon-search']").click()
time.sleep(8)
slider_container = driver.find_element(By.XPATH,value="//div[#class='slide-verify-slider']")
slider = driver.find_element(By.XPATH, value="//div[#class='slide-verify-slider-mask-item']")
# Perform sliding action
for x in range(10000):
actions.move_to_element(slider).click_and_hold().move_by_offset(x, 0).release().perform()
time.sleep(0.1)
To resolve captcha you must release button in correct place, not just slide.
Much better way to get data is to use REST API
REST API
I'm trying to input an email address to test logging in, but I'm continuing to receive this error: no such element: Unable to locate element:
I have tried using the relative and absolute Xpath and receive the same error message.
Forgive me as I'm sure missing something simple, very new to this!
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import time
driver = webdriver.Chrome()
url = 'https://soundcloud.com/signin'
driver.get(url)
time.sleep(2)
driver.find_element_by_xpath('//*[#id="sign_in_up_email"]').send_keys('test#test.com')
The reason its throwing error is because the element sign_in_up_email is present inside the iframe
Refer image
Check the link here for detail about how to switch to iframe
You will first need to switch to iframe and then enter value in the input
Note:- When you first open the page you might see the accept cookies popup from soundcloud you will have to accept that
Your solution would look like
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import time
driver = webdriver.Chrome()
url = 'https://soundcloud.com/signin'
driver.get(url)
driver.maximize_window()
time.sleep(2)
# Accept cookie
driver.find_element_by_id('onetrust-accept-btn-handler').click()
# Switch to frame
driver.switch_to.frame(driver.find_element_by_class_name("webAuthContainer__iframe"))
driver.find_element_by_xpath('//*[#id="sign_in_up_email"]').send_keys('test#test.com')
Ive been attempting to use selenium to go through elements on soundclouds website and am having trouble interacting with the input tags. When I try to write in the input tag of the class "headerSearch__input" with the send keys command, I get back the error "Message: element not interactable". May someone please explain to me what im doing wrong?
from tkinter import *
import random
import urllib.request
from bs4 import BeautifulSoup
from selenium import webdriver
import time
import requests
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
driver = webdriver.Chrome(executable_path='/Users/quanahbennett/PycharmProjects/SeleniumTest/chromedriver')
url= "https://soundcloud.com/"
driver.get(url)
#time.sleep(30)
wait = WebDriverWait(driver, 30)
#link = driver.find_elements_by_link_text("Sign in")
#link[0].click()
#driver.execute_script("arguments[0].click();", link[0])
#SUCCESFUL LOGIN BUTTON PUSH
#please = driver.find_element_by_css_selector('button.frontHero__loginButton')
#please.click()
attempt = driver.find_element_by_css_selector('input.headerSearch__input')
time.sleep(10)
attempt.send_keys('Hello')
breakpoint()
#driver.quit()
The locator - input.headerSearch__input is highlighting two different elements in the DOM. Its important to find unique locators. Link to refer
And also close the cookie pop-up. And then try to interact with elements.
Try like below and confirm.
driver.get("https://soundcloud.com/")
wait = WebDriverWait(driver,30)
# Click on Accept cookies button
wait.until(EC.element_to_be_clickable((By.ID,"onetrust-accept-btn-handler"))).click()
search_field = wait.until(EC.element_to_be_clickable((By.XPATH,"//div[#id='content']//input")))
search_field.send_keys("Sample text")
i am trying to using the selenium auto input the HTML code in http://ueditor.baidu.com/website/examples/completeDemo.html. My procedure is that click the html first, and then code HTML in, while the IDE always told me that cant locate the element. it mad me crazy that the after click the HTML Button, the element is right there, but always error on. i just wonder how can i write in box after click the HTML button by using selenium? thanks to the warm-hearted guy
import os,time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
chromePath = r'E:/Python/WEB/web-infor-transfer/monidenglu/chromedriver.exe'
wd = webdriver.Chrome()
loginUrl = 'http://ueditor.baidu.com/website/examples/completeDemo.html'
wd.get(loginUrl)
wd.find_element_by_xpath('//*[#id="edui4"]').click()
time.sleep(2)
wd.find_element_by_xpath('//*[#id="edui1_iframeholder"]/div/div[2]/div/div/div[2]/div/div[2]').send_keys('hello')
time.sleep(5)
wd.quit()
Try action chains. For example - sending keys to the browser itself worked:
wd.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/div[2]/div/div/div[2]/div/div[2]/pre[2]/span').click()
actions = ActionChains(wd)
actions.send_keys('hello')
actions.perform()