I want to save element data to an excel file via python. I have the code below, I need some help why the line where
element.click()
gives an error. Even though I put the click() method upper line, but i need it to be in line below.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(r"C:\Users\Admin\Downloads\chromedriver_win32 (1)\chromedriver.exe")
driver.get("https://www.nba.com/schedule?pd=false®ion=1")
driver.implicitly_wait(30)
element_to_click=driver.find_element(By.ID,"onetrust-accept-btn-handler").click()
element_to_click.click() 'error
element_to_save=driver.find_element(By.XPATH,"//div/div/div/div/h4")
#element_to_save.to_excel("3row,3column)")
driver.quit()
This is one way to reject/accept cookies on that website:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1280,720")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(browser, 20)
url = 'https://www.nba.com/schedule?pd=false®ion=1'
browser.get(url)
try:
wait.until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
print('accepted cookies')
except Exception as e:
print('no cookie button!')
Setup is selenium/chrome on linux - just observe the imports and the part after defining the browser/driver.
Selenium documentation can be found at https://www.selenium.dev/documentation/
Related
I'm using selenium to get to YouTube and write something on the search bar and then press the button or press the enter key.
Both clicking or pressing a key does sometimes work, but sometimes it does not.
I tried to wait with WebDriverWait, and I even changed the waiting time from 10 to 20 seconds, but it didn't make any difference.
And if I add anything (like printing the new page title), it only shows me the first page title and not the title after the search.
Here is my code and what I tried:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def get_driver():
firefox_options = Options()
# firefox_options.add_argument("--headless")
driver = webdriver.Firefox(executable_path=r"C:\Program Files\Mozilla Firefox\geckodriver.exe", options=firefox_options)
driver.implicitly_wait(9)
return driver
driver = get_driver()
driver.get('https://www.youtube.com/')
search = driver.find_element(By.XPATH, '//input[#id="search"]')
search.send_keys("python")
# search.send_keys(Keys.ENTER) #using the enter key # If I add nothing after this line it work
# searchbutton = driver.find_element(By.XPATH,'//*[#id="search-icon-legacy"]') # This also dose doesn't work
# searchbutton.click() # using the click method() #also dose not work
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="search-icon-legacy"]'))).click() # Sometimes work
# driver.implicitly_wait(10)
# print(driver.title) # This show me only the title of the first page not the one after the search
Is it because I use the Firefox webdriver (should I change to Chrome)?
Or is it because of my internet connection?
To make this working you need to click the search field input first, then add a short delay and then send the Keys.ENTER or click search-icon-legacy element.
So, this is not your fault, this is how YouTube webpage works. You may even call it a kind of bug. But since this webpage it built for human users it works good since human will never click on the input field and insert the search value there within zero time.
Anyway, the 2 following codes are working:
First.
import time
from selenium import webdriver
from selenium.webdriver import Keys
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")
options.add_argument('--disable-notifications')
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://www.youtube.com/"
driver.get(url)
search = wait.until(EC.element_to_be_clickable((By.XPATH, '//input[#id="search"]')))
search.click()
time.sleep(0.2)
search.send_keys("python")
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="search-icon-legacy"]'))).click()
Second.
import time
from selenium import webdriver
from selenium.webdriver import Keys
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")
options.add_argument('--disable-notifications')
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://www.youtube.com/"
driver.get(url)
search = wait.until(EC.element_to_be_clickable((By.XPATH, '//input[#id="search"]')))
search.click()
time.sleep(0.2)
search.send_keys("python" + Keys.ENTER)
I am trying to locate a button that uploads a file and gets the ouput result by clicking the button on the page itself, I know how to upload file by send keys.
The website is https://huggingface.co/spaces/vaibhavsharda/semantic_clustering
My code is
import csv
import time
from selenium import webdriver
import chromedriver_autoinstaller
import datetime
from bs4 import BeautifulSoup
from selenium.webdriver 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
chromedriver_autoinstaller.install() # Check if the current version of chromedriver exists
# and if it doesn't exist, download it automatically,
# then add chromedriver to path
driver = webdriver.Chrome()
count = 0
entire_data = []
driver.get("https://huggingface.co/spaces/vaibhavsharda/semantic_clustering")
driver.maximize_window()
time.sleep(10)
s = driver.find_element(By.CSS_SELECTOR,'.exg6vvm15 .edgvbvh9') # the error is here.
s.send_keys("small_test.txt")
I am trying to locate element by selenium, I don't know if it doesn't load or something else is the error but I just want to locate the "Browse Files" button. Feel free to ask me anything.
Element you trying to click is inside an iframe, so you need first to switch into the iframe in order to access that element.
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(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 5)
url = "https://huggingface.co/spaces/vaibhavsharda/semantic_clustering"
driver.get(url)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title]")))
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#kind='primary'][not(#disabled)]"))).click()
When finished don't forget to switch to the default content with:
driver.switch_to.default_content()
UPD
Uploading file with Selenium is done by sending the uploaded file to a special element. This is not an element you are clicking as a user via GUI to upload elements. The element actually receiving uploaded files normally matching this XPath: //input[#type='file']
This is the fully working code - I tried this on my PC uploading some text file.
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(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 5)
url = "https://huggingface.co/spaces/vaibhavsharda/semantic_clustering"
driver.get(url)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title]")))
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input[type='file']"))).send_keys("C:/project_name/.gitignore")
I'm having a strange issue with Python Selenium when trying to click on the next page link in Curry's PC World Televisions page. See the next button on (https://www.currys.co.uk/tv-and-audio/televisions/tvs)
I am testing in non-headless mode with chrome, so i can see the outline of the next button being highlighted, when actioning the click, but it is not navigating to the next page url as expected.
Here is what i have tried on the page above:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
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.common.exceptions import NoSuchElementException
import time
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver.set_window_size(1920, 1080)
driver.maximize_window()
driver.get('https://www.currys.co.uk/tv-and-audio/televisions/tvs')
next_page_link = WebDriverWait(driver, 10).until(
ec.element_to_be_clickable((By.CSS_SELECTOR,
'ul.pagination '
'li.pagination-arrrow-next a.page-next')))
next_page_link.click()
time.sleep(10)
I have also tried using execute script, (which does not work either):
driver.execute_script('arguments[0].click()', next_page_link)
I am able to extract the href from the link element (so i know its the right element):
href = next_page_link.get_attribute('href')
print("href is: {}".format(href))
# href is: https://www.currys.co.uk/tv-and-audio/televisions/tvs?start=20&sz=20
I'm a bit lost as to what the issue might be here, would appreciate any help!
I see the following issues here:
You have to close the accept cookies pop-up.
You need to scroll the page down to the pager elements.
Selenium indeed doesn't make the page to go to the next page BUT even when I click the Next button manually or even any other pagination button there manually the page is not changed. So, the problem is with the web page itself, not with Selenium.
This is the code I used:
import time
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.currys.co.uk/tv-and-audio/televisions/tvs'
driver.get(url)
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
next_page_link = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'ul.pagination li.pagination-arrrow-next a.page-next')))
next_page_link.location_once_scrolled_into_view
time.sleep(1)
next_page_link.click()
I'm trying to write a script that fills out the destination box on the google flights page. My code is behaving very inconsistently. Sometimes it works perfectly, other times it only types the letter 'B', rather than the full string 'Barcelona'. Sometimes, I get this error message:
"element click intercepted: Element (redacted html code) is not clickable at point (547, 472). Other element would receive the click"
Any idea why it's freaking out like this? Here's my code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
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.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import os
#Give path to chrome driver using service argument so it doesn't throw the path deprecation warning
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
chromedriver_path = '/path/to/chromedriver'
abs_chromedriver_path = os.path.join(script_dir, chromedriver_path)
driver_service = Service(executable_path = abs_chromedriver_path)
browser = webdriver.Chrome(service = driver_service)
url = 'https://www.google.com/travel/flights'
selector = "div[aria-placeholder='Where from?'] input"
phrase = "Barcelona"
browser.get(url)
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector))).click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector))).send_keys(phrase)
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector))).send_keys(Keys.ENTER)
This is one way to look for a flight from Barcelona to Vancouver, select the first suggestion from the suggestion lists for both starting/endpoint, and clicking 'Search':
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time as t
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1280,720")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
url = 'https://www.google.com/travel/flights'
browser.get(url)
try:
cookie_button = WebDriverWait(browser, 3).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'button[aria-label="Reject all"]')))
print(cookie_button.location_once_scrolled_into_view)
t.sleep(1)
cookie_button.click()
print('rejected cookies')
except Exception as e:
print('no cookie button')
t.sleep(1)
comboboxes = WebDriverWait(browser, 3).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'input[role="combobox"]')))
comboboxes[0].click()
comboboxes[0].clear()
comboboxes[1].send_keys('Barcelona')
t.sleep(1)
suggestion_listbox = WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "ul[role='listbox']")))
print(suggestion_listbox[-1].get_attribute('outerHTML'))
suggestion_listbox[-1].find_elements(By.TAG_NAME, 'li')[0].click()
comboboxes[2].click()
comboboxes[3].send_keys('Vancouver')
t.sleep(1)
suggestion_listbox = WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "ul[role='listbox']")))
print(suggestion_listbox[-1].get_attribute('outerHTML'))
suggestion_listbox[-1].find_elements(By.TAG_NAME, 'li')[0].click()
t.sleep(1)
WebDriverWait(browser, 3).until(EC.element_to_be_clickable((By.XPATH,'//span[text() = "Search"]'))).click()
You just have to adapt it to your own selenium setup. Selenium docs: https://www.selenium.dev/documentation/
I would like to scroll a container within a webpage - not the webpage itself - using selenium within Python. Would anyone have any solutions to this?
I have tried the standard:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
Of course, this is fine for traversing the full web page, but not a container itself.
EDIT: The webpage is https://www.mixcloud.com/
The container shows when you use the search bar at the top of the page.
This is one way to scroll the page (it will stop once it goes over 300 songs):
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui 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
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchShadowRootException, NoSuchElementException
import time as t
import pandas as pd
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1280,720")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
actions = ActionChains(browser)
wait = WebDriverWait(browser, 20)
url = "https://www.mixcloud.com/"
browser.get(url)
s_box = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[name="mixcloud_query"]')))
s_box.click()
s_box.send_keys('house')
s_box.send_keys(Keys.ENTER)
music_div = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'body[class="rebrand"]')))
music_div.click()
while True:
t.sleep(5)
music_div.send_keys(Keys.END)
print('scrolled to bottom')
songs = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'div[class^="SearchAudioCard__SearchAudioCardContainer"]')))
print('songs:', len(songs))
print('______________-')
if len(songs) > 300:
print('got over 300 songs, stopping')
break
browser.quit()
Selenium documentation can be found at https://www.selenium.dev/documentation/