My goal is to disable cookies when I access page https://www.icribis.com/it/ (that is click on the button "Rifiuta"). My code, which is not working, is:
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
import time
url = 'https://www.icribis.com/it/'
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get(url)
time.sleep(5)
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="uc-center-container"]/div[2]/div/div[1]/div/div[2]/button[2]'))).click()
time.sleep(5)
driver.close()
I found the XPath by inspecting the element on the web page.
How can I correct it?
It's in shadow-root.
You will have to use execute_script
url = 'https://www.icribis.com/it/'
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get(url)
time.sleep(5)
cookie_dsbl_btn = driver.execute_script('return document.querySelector("#usercentrics-root").shadowRoot.querySelector("#uc-center-container > div:nth-child(2) div > button:nth-child(3)")')
cookie_dsbl_btn.click()
time.sleep(5)
Related
I'm using selenium to try and scrape a listing of products in this website:
https://www.zonacriativa.com.br/harry-potter
However, I'm having trouble getting the full listing of products. the page list 116 products, yet only a few are shown at a time. If I want to see the other ones, I need to click on the "Carregar mais Produtos" (load more products) button at the bottom a few times to get the full listing.
I'm having trouble locating this button, as it doesn't have an id and its class is a huge string. I've tried several things, like the examples below, but they don't seem to work. Any suggestions?
driver.find_element("xpath", "//button[text()='Carregar mais Produtos']").click()
driver.find_element("css selector", ".vtex-button__label.flex.items-center.justify-center.h-100.ph5").click()
driver.find_element(By.CLASS_NAME, "vtex-button.bw1.ba.fw5.v-mid.relative.pa0.lh-solid.br2.min-h-small.t-action--small.bg-action-primary.b--action-primary.c-on-action-primary.hover-bg-action-primary.hover-b--action-primary.hover-c-on-action-primary.pointer").click()
The element you trying to click is initially out of the visible screen so you can't click it. Also this XPath at least for me doesn't locate that element.
What you need to do is to scroll the page down untill that button becomes visible and clickable and then click it.
The following code clicks that button 1 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(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 5)
url = "https://www.zonacriativa.com.br/harry-potter"
driver.get(url)
while True:
try:
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(#class,'buttonShowMore')]//button"))).click()
break
except:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
The above code can be simply modified to scroll and click that button until we reach the latest page where this button is not presented:
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://www.zonacriativa.com.br/harry-potter"
driver.get(url)
while driver.find_elements(By.XPATH, "//div[contains(#class,'buttonShowMore')]//button"):
try:
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(#class,'buttonShowMore')]//button"))).click()
except:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
When I run the code, it opens the browser, but I want to click the reject all button when the cookie banner comes from YouTube. I tried using class and a link text. It did not work.
I will appreciate any help.
from selenium import webdriver
from selenium.webdriver.common.by import By
url = 'https://www.youtube.com/'
driver = webdriver.Chrome(executable_path="C:\\Users\\donner\\Downloads\\chromedriver_win32\\chromedriver.exe")
driver.get(url)
driver.implicitly_wait(100)
continue_link = driver.find_element(By.LINK_TEXT, "Reject all")
continue_link.click()
driver.implicitly_wait(100)
content = driver.find_element(By.CLASS_NAME, '.style-scope.ytd-button-renderer.style-primary size-default')
content.click()
Try the below xpath with the given code
//*[normalize-space()='Reject all']
OR
//*[contains(text(),'Reject all')]
The Code:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "myXpath")))
element.click();
The above soultion work if there is no iframe
In case there is iframe, than you need to follow below
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframe")))
driver.find_element(By.XPATH, "yourXpath").click()
The code is as below.
driver = webdriver.Chrome(chromedriver_path) #webdriver path
driver.get('https://webtoon.kakao.com/content/%EB%B0%94%EB%8B%88%EC%99%80-%EC%98%A4%EB%B9%A0%EB%93%A4/1781') #website access
time.sleep(2)
driver.execute_script("window.scrollTo(0, 900)") #scroll down
time.sleep(1)
However, the page does not scroll.
How can I scroll?
Page link to be scrolled
Try this
driver.execute_script("window.scrollTo(100,document.body.scrollHeight);")
Tried with the below code, it did scroll.
driver.get("https://webtoon.kakao.com/content/%EB%B0%94%EB%8B%88%EC%99%80-%EC%98%A4%EB%B9%A0%EB%93%A4/1781")
time.sleep(2)
options = driver.find_element_by_xpath("//div[#id='root']/main/div/div/div/div[1]/div[3]/div/div/div[1]/div/div[2]/div/div[1]/div/div/div/div")
driver.execute_script("arguments[0].scrollIntoView(true);",options)
The webapp is dynamic (which is, the more you scroll down, more you will see the data), you can perform infinite scrolling like below :-
driver = webdriver.Chrome(chromedriver_path) #webdriver path
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://webtoon.kakao.com/content/%EB%B0%94%EB%8B%88%EC%99%80-%EC%98%A4%EB%B9%A0%EB%93%A4/1781') #website access
time.sleep(2)
wait = WebDriverWait(driver, 20)
time.sleep(5)
action = ActionChains(driver)
i = 3
while True :
action.move_to_element(driver.find_element(By.XPATH, f"(//div[contains(#class, 'AspectRatioBox_aspectRatioBox')])[{i}]")).perform()
i = i +1
time.sleep(.5)
Imports :
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.action_chains import ActionChains
I am trying to do a tutorial and learn Selenium in python however i cant seem to get Selenium to click the "Add To Cart" button using either find_element_by_class or find_element_by_XPATH. The problem is to check if the item is out of stock, and if it is out of stock then to refresh the webpage and restart the script. If the item is in stock then it should click "Add To Cart"
I am using:
Python v3.9
Chrome v87
This is the URL i am practicing on:
https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html
And this is my current code for the clicking:
# Selenium Tutorial #1
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
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome(r"C:\Users\Ste1337\Desktop\chromedriver\chromedriver.exe")
driver.get("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html")
#search = driver.find_element_by_id(ContentPlaceHolder1_NotifyBtn)
link = driver.find_element_by_id("onetrust-accept-btn-handler")
link.click
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler"))
)
element.click()
except Exception:
pass
driver.implicitly_wait(2)
try:
element = WebDriverWait(driver, 1).until(
EC.presence_of_element_located((By.XPATH, "email-desktop"))
)
time.sleep(1)
browser.refresh()
except:
driver.find_element_by_class_name("Button__StyledButton-bvTPUF hZIOeU Button-jyKNMA GZkwS")
link.click()
You cannot pass multiple classes to find_element_by_class_name. Instead use driver.find_element_by_css_selector or xpath.
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
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome(r"C:\Users\Frank\Documents\Python Scripts\chromedriver_win32\chromedriver.exe")
driver.get("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html")
#search = driver.find_element_by_id(ContentPlaceHolder1_NotifyBtn)
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler"))
)
element.click()
except Exception:
pass
driver.implicitly_wait(2)
try:
element = WebDriverWait(driver, 1).until(
EC.presence_of_element_located((By.XPATH, "email-desktop"))
)
time.sleep(5)
driver.refresh()
except:
button = driver.find_element_by_css_selector("button.Button__StyledButton-bvTPUF.hZIOeU.Button-jyKNMA.GZkwS")
Next, if you would call button.is_displayed() it will return False, because it is a hidden element. This means you cannot interact with it using button.click(). Instead, you can use javascript to click the button.
driver.execute_script("arguments[0].click();", button)
I'm using selenium to browse this page:
https://webapps.cityofchicago.org/activegcWeb/
But I can't find how move to any page, with chrome I get this Xpath for the 'next' button:
'//*[#id="id2"]/a[3]'
I'm using this code:
url = 'https://webapps.cityofchicago.org/activegcWeb/'
driver_1 = webdriver.Firefox()
driver_1.get(url)
content = driver_1.page_source
next_button_xpath = '//*[#id="id2"]/a[3]'
button = driver_1.find_element_by_xpath(next_button_xpath)
button.click()
But I got this error:
'selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//*[#id=\"id2\"]/a[3]"}'
With XPath locator "//a[contains(#href, 'headerPaginator:next')]" and then just click.
Just find the next button by the link text:
driver.find_element_by_link_text(">").click()
Complete working code (including the window maximizing and the wait):
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
url = 'https://webapps.cityofchicago.org/activegcWeb/'
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)
driver.maximize_window()
driver.get(url)
# click next
wait.until(EC.visibility_of_element_located((By.LINK_TEXT, ">"))).click()