I am using Selenium to remove a PopUp from investing.com but i am not able to recognise the PopUp correctly. The code I am using is ;
I need to click the button "Got it"...
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.common.exceptions import TimeoutException
from time import sleep
browser = webdriver.Chrome(executable_path="C:\\…….\chromedriver.exe")
browser.get("https://uk.investing.com/indices/mining-historical-data/")
browser.switchTo().frame(browser.findElement(By.xpath("//iframe[contains(#src,'https://prefmgr- cookie.truste-svc.net/cookie_js/cookie_iframe.html?parent=https://consent-pref.trustarc.com/?? layout=gdpr&type=investingiab2&site=investing-iab.com&action=notice&country=gb&locale=en&behavior=expressed&uid=dc7cd041-d8c4-4102-9522-1025da9b6e09&privacypolicylink=https://uk.investing.com/about-us/privacy-policy&iab=true&irm=undefined&from=https://consent.trustarc.com/')]"")));
sleep(3)
browser.findElement(By.xpath("//input[#name='Got it']")).click();
The popup is inside an iframe and elements ids change so it is a bit challenging.
Sometimes a second popup appears, make sure run this code before it.
This code is for closing the first popup.
from selenium import webdriver
from time import sleep
browser = webdriver.Chrome(executable_path="C:\\…….\chromedriver.exe")
browser.get("https://uk.investing.com/indices/mining-historical-data/")
sleep (3)
# get iframe element
title = "TrustArc Cookie Consent Manager"
frameElement = browser.find_element_by_xpath("""//*[#title="TrustArc Cookie Consent Manager"]""")
# get id iframe
id = frameElement.get_property("id")
print(id)
# switch to iframe element
browser.switch_to.frame(id)
sleep (1)
# get element to click
elemet = browser.find_element_by_xpath("""//*[#class='call']""")
print(elemet.get_attribute('innerHTML'))
# click element
elemet.click()
# switch the control back to the parent frame
browser.switch_to.default_content()
Related
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
Hi i'm new at selenium and webscraping and i need some help.
i try to scrape one site and i need and i dont know how to get span class.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATCH = "/Users/bobo/Downloads/chromedriver"
driver = webdriver.Chrome(PATCH)
driver.get("https://neonet.pl")
print(driver.title)
search = driver.find_element_by_class_name("inputCss-input__label-263")
search.send_keys(Keys.RETURN)
time.sleep(5)
i try to extract this span
<span class="inputCss-input__label-263">Szukaj produktu</span>
I can see that you are trying to search something in the search bar.
First I recommend you to use the xpath instead of the class name, here is a simple technique to get the xpath of every element on a webpage:
right-click/ inspect element/ select the mouse in a box element on the upper left/ click on the element on the webpage/ it will directly show you the corresponding html/ then right click on the selected html/ copy options and then xpath.
Here is a code example that searches an element on the webpage, I also included the 'Webdriver-wait' option because sometimes the code runs to fast and can't find the next element so this function make the code wait till the element is visible:
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path="/Users/bobo/Downloads/chromedriver")
driver.get("https://neonet.pl") #loading page
wait = WebDriverWait(driver, 20) #defining webdriver wait
search_word = 'iphone\n' # \n is going to act as an enter key
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="root"]/main/div[1]/div[4]/div/div/div[2]/div/button[1]'))).click() #clicking on cookies popup
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="root"]/main/header/div[2]/button'))).click() #clicking on search button
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="root"]/aside[2]/section/form/label/input'))).send_keys(search_word) #searching on input button
print('done!')
sleep(10)
Hope this helped you!
wait=WebDriverWait(driver,10)
driver.get('https://neonet.pl')
elem=wait.until(EC.visibility_of_element_located((By.XPATH, "//span[contains(#class,'inputCss-input')]"))).text
print(elem)
To output the value of the search bar you use .text on the Selenium Webelement.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I am trying to close a pop up window with selenium in python, which is not allowing my code to execute further. But I am unable to do that. There is a a pop up window which asks me if i want to sign up but it keeps popping up at inconsistent times. Is there a method to check wether or not the pop-up window is active?
My code so far:
import selenium
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
import time
PATH = "C:\Program Files (x86)\chromedriver.exe";
driver = webdriver.Chrome(PATH);
driver.get("https://www.investing.com/news/")
time.sleep(3)
accept_cookies = driver.find_element_by_xpath('//*[#id="onetrust-accept-btn-handler"]');
accept_cookies.click();
So your problem is that you don't want the signup popup to display.
I was just poking the site's script, I found a really nice way for you to work around it: set user agent to gene. The script to display popup will be disabled if user agent matches some mobile browsers.
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options()
opts.add_argument("user-agent=gene")
driver = webdriver.Chrome(options=opts)
# Adds the cookie into current browser context
driver.get("https://www.investing.com/news/")
time.sleep(60) # wait for a minute to see if the popup happens
driver.quit()
Run this code, observe the page, it will not display the signup popup.
Alternative work around:
Use a Chrome profile that already turned off the Signup popup, will also not display the popup
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
If you really need to close the popup without these methods, define a check function with try/except. Before doing any actions, call this function to check if there's popup, then close the popup, save the state to some variable (so you don't check it anymore). Since the function has try/except, it will not raise Exception and your code will run.
You can do this with 2 quick methods that I can think of immediately.
1:
You will use this often
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
then when you want to get an element that needs time to load and you want to do actions with it, you implement it with WebDriverWait :
wait = WebDriverWait(driver, 10)
try:
accept_cookies = wait.until(EC.presence_of_element_located((By.XPATH, "'//*
[#id=\"onetrust-accept-btn-handler\"]'")))
catch:
# probably you have a bad locator and the element doesn't exist or it needs more than 10 sec to load.
else:
# logic here is processed after successfully obtaining the pop up
accept_cookies.click()
I would recommend using ActionChains
from selenium.webdriver.common.action_chains import ActionChains
then after you obtain the pop up element make the click like this:
actions = ActionChains(driver)
actions.move_to_element(accept_cookies).click().perform()
This is the equivalent of moving the mouse pointer to the middle of the pop up close btn and clicking it
I am Trying to create a bot that can fill a cart with these beer bottles. I really want to do this on a few different sites but for some reason i can only get it to open the page and click the first button, then it doesn't click the next button. I tried ID, Name, pretty much anyway to identify the button but it won't click it. I even tried sleep for 3 seconds. I tried to see if it was in an Iframe but i don't think it is. I'm out of ideas.... Link is https://www.sideprojectbrewing.com/shop?category=Beer+Release
I'm trying to access the add to cart element but does not seem to work
\
from Config import keys
from selenium import webdriver
def order(k):
driver = webdriver.Chrome(executable_path=r"C:\Users\ezliv\Desktop\ShopBot1\chromedriver_win32\chromedriver.exe")
driver.get(k['product_url'])
driver.find_element_by_xpath('//*[#id="thumb-biereblanche"]/div/div[1]/div/div/img').click()
driver.find_element_by_xpath('//*[#id="yui_3_17_2_1_1606181545139_755"]').click()
\\
You can try following code:
driver.get(url_here)
wait = WebDriverWait(driver, 20)
bottle = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="thumb-biereblanche"]/div/div[1]/div/div/img')))
bottle.click()
add_to_cart = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'sqs-add-to-cart-button-inner')))
add_to_cart.click()
Import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I'm trying to log in to a webpage so that I can scrape data from it, but every time I try, i get the error Element Not Visible. The element I'm trying to click is a button inside of a modal window, but the html for that window only appears after a login button is clicked. I can click the login button, but I can't click the teacher button that pops up afterwards. Any ideas?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
login_url = 'https://edpuzzle.com/'
username = '***'
password = '***'
ChromeDriver = r'C:\Users\admin\Python\chromedriver.exe'
driver = webdriver.Chrome(ChromeDriver)
driver.get(login_url)
driver.find_element_by_css_selector('button.btn.btn-default-
transparent').click()
driver.find_element_by_css_selector('p.modal-title.text-lg.text-center.edp-
role-title')
driver.find_element_by_css_selector('button.btn.white-btn.btn-lg.btn-
block.big-btn.edp-teacher-role').click()
You're trying to click button before it actually visible. You need to wait some time:
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver.get(login_url)
driver.find_element_by_id('edp-login').click()
wait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn.white-btn.btn-lg.btn-block.big-btn.edp-teacher-role"))).click()