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
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);")
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)
This is my code and it works completely fine for selecting size but it does not click the add to cart button as I can not see the product being added to my cart.
from selenium import webdriver as wd
import chromedriver_binary
from selenium.webdriver.common.action_chains import ActionChains
wd= wd.Chrome()
action= ActionChains(wd)
wd.implicitly_wait(1)
url="https://www.nike.com/ca/launch/t/air-max-pre-day-pure-platinum"
wd.get(url)
#wd.maximize_window()
size=wd.find_element_by_xpath('//*[#id="root"]/div/div/div[1]/div/div[1]/div[2]/div/section[1]/div[2]/aside/div/div[2]/div/div[2]/ul/li[15]/button')
size.click()
wd.implicitly_wait(1)
cart=wd.find_element_by_xpath('//*[#id="root"]/div/div/div[1]/div/div[1]/div[2]/div/section[1]/div[2]/aside/div/div[2]/div/div[2]/div/button')
action.move_to_element(cart)
action.click()
action.perform()
do not comment this :
#wd.maximize_window()
you are using absolute xpaths, which seems to be locating the right element, I would suggest you to write relative xpath and with Expected conditions :
and you need to scroll also :
driver.execute_script("window.scrollTo(0, 250)")
and then do this :
size=wd.find_element_by_xpath('//*[#id="root"]/div/div/div[1]/div/div[1]/div[2]/div/section[1]/div[2]/aside/div/div[2]/div/div[2]/ul/li[15]/button')
size.click()
Update 1 :
driver = webdriver.Chrome()
driver.maximize_window()
wait = WebDriverWait(driver, 10)
driver.implicitly_wait(10)
driver.get("https://www.nike.com/ca/launch/t/air-max-pre-day-pure-platinum")
sleep(10)
driver.execute_script("window.scrollTo(0, 750)")
action = ActionChains(driver)
action.move_to_element(wait.until(EC.visibility_of_element_located((By.XPATH, "//figure[contains(#data-qa,'card-product-image-3')]")))).perform()
action.move_to_element(wait.until(EC.visibility_of_element_located((By.XPATH, "//figure[contains(#data-qa,'card-product-image-5')]")))).perform()
action.move_to_element(wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='US 10.5']")))).click().perform()
action.move_to_element(wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='add to bag']")))).click().perform()
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
See if this works:-
cart = driver.find_element_by_xpath(".//button[text()='add to bag']")
driver.execute_script("arguments[0].scrollIntoView(true);", cart)
cart.click()
I am trying to scrape some google data but I first want to click the 'I agree' button that google pops up. This is the script I use to do that:
import time
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
search_question = input("Ask a question: ")
driver = webdriver.Chrome("*Your Webdriver location*")
driver.wait = WebDriverWait(driver, 5)
driver.get("https://google.com")
time.sleep(1)
agree = driver.wait.until(EC.presence_of_element_located((By.XPATH, '//*[#id="introAgreeButton"]/span/span')))
agree.click()
# time.sleep(0.2)
search = driver.find_element_by_class_name("gLFyf")
search.send_keys(search_question)
search.send_keys(Keys.ENTER)
The problem is selenium doesn't seem to locate the button and therefore I get a timeout error. (I have tried also with find_element_by_xpath and still not working).
If you scroll up in the devtools inspector you'll notice that your element is within an iframe:
You need to switch to that frame first, click your button then switch back to the default content (the main page)
driver.get("https://google.com")
#active the iframe and click the agree button
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe")))
agree = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="introAgreeButton"]/span/span')))
agree.click()
#back to the main page
driver.switch_to_default_content()
That works for me.
FYI - There's only 1 iframe on the page, that's why the xpath //iframe works. If there were multiple you'd need to identify it with higher accuracy.
If you already have been agreed,then agree button would not appear. That why it is not be able to find given XPath.
Try this:
import time
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
search_question = input("Ask a question: ")
driver = webdriver.Chrome(".\chromedriver.exe")
driver.wait = WebDriverWait(driver, 5)
driver.get("https://google.com")
time.sleep(3)
# agree = driver.wait.until(EC.presence_of_element_located((By.XPATH, '//*[#id="introAgreeButton"]/span/span')))
# agree.click()
# time.sleep(0.2)
search = driver.find_element_by_class_name("gLFyf")
search.send_keys(search_question)
search.send_keys(Keys.ENTER)
Managed to get it working.
Seems like one of the few elements that are interactive in the popup when you first load the site is the language dropdown, which I found by class name.
Then the Agree button is detectable and you can find it by class too.
driver.get("https://www.google.com")
driver.maximize_window()
time.sleep(2)
#finds+clicks language dropdown, interaction unhides the rest of the popup html
driver.find_element_by_class_name('tHlp8d').click()
time.sleep(2)
#finds+clicks the agree button now that it has become visible
driver.find_element_by_id('L2AGLb').click()
you should now have the standard searchbar etc
i had some problems with clicking pop-ups and the problem turned out to be with that click().
im not sure that it might be the same issue with urs or not but try changing ur click to this :
agree = driver.find_element_by_xpath('//*[#id="introAgreeButton"]/span/span')
driver.execute_script("arguments[0].click();", agree)
The problem was that it didn't change frames here is the soulution code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome('C:\\Users\\gassp\\OneDrive\\Namizje\\Python.projects\\chromedriver.exe')
url = 'https://www.google.com/maps/'
driver.get(url)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[#id="consent-bump"]/div/div[1]/iframe')))
agree = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="introAgreeButton"]/span/span')))
agree.click()
#back to the main page
driver.switch_to_default_content()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="searchboxinput"]'))).send_keys('gostilne')
driver.submit()
I've tried to keep clicking on more button located at the bottom of a webpage (in it's landing page) to unveil all the headlines. The thing is when I execute my script, It only click once and then stop. How can I keep clicking on that button until there is no more option to click?
Link to that website
This is my script so far:
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
link = "https://www.newsnow.co.uk/h/Sport/Football/Championship/Transfer+News"
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get(link)
while True:
try:
loadmore = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[class^='hl_more']")))
driver.execute_script("arguments[0].scrollIntoView();",loadmore)
loadmore.click()
except Exception: break
driver.quit()
Try below code to simulate required behavior:
header = driver.find_element_by_id("phead")
driver.execute_script('arguments[0].style.position = "absolute";', header)
while True:
try:
loadmore = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.hl_more.bg_tween:not(.nfloading)")))
driver.execute_script("arguments[0].scrollIntoView();",loadmore)
loadmore.click()
except: break