Scroll to a button with Selenium Python - python

I'm working on a webscraping script for a school project. I have to scroll down to a button to click, but I can't make it work.
I tried the following code:
driver.get('https://www.goodreads.com/book/show/28187.The_Lightning_Thief');
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div/div[1]/div/div/button')))
button.click()
time.sleep(5)
element = driver.find_element(By.CLASS_NAME,"Button Button--transparent Button--small")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '''//*[#id="ReviewsSection"]/div[5]/div/div[4]/a''')))
button.click()
I recieved the following error:
Message: no such element: Unable to locate element: {"method":"css selector","selector":".Button Button--transparent Button--small"}
I also tried this, but it didn't work either:
driver.execute_script("arguments[0].scrollIntoView();", element)
When you scroll down on this page it will load in more reviews, so I guess that the button in the bottom I want to click don't load in, so I also tried to scroll down a few times and then try to scroll to the element, but it didn't work.
Can someone please help out?
EDITED:
I also tried this code:
driver = webdriver.Chrome()
driver.get('https://www.goodreads.com/book/show/13335037-divergent');
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div/div[1]/div/div/button')))
button.click()
time.sleep(3)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
element = driver.find_element(By.CSS_SELECTOR, "div.Divider.Divider--contents.Divider--largeMargin")
driver.execute_script("arguments[0].scrollIntoView();", element)
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '''//*[#id="ReviewsSection"]/div[5]/div/div[4]/a''')))
button.click()
This will scroll down to the bottom once end let the remaining reviews load in, then get the element and scroll again. But it's not scrolling down enough so the See all reviews and ratings won't be in view and can't be clicked.

Your selector is invalid since you're trying to pass multiple class names to search by class. You need to pass single #class only or to use another locator type, e.g.
element = driver.find_element(By.CSS_SELECTOR, ".Button.Button--transparent.Button--small")
P.S. Note that there are 4 elements with the same set of class names ("Button Button--transparent Button--small"), so search by class names is not a good option in this case
element = driver.find_element(By.XPATH, '//a[.="See all reviews and ratings"]')

Related

Python Selenium move to element

driver = webdriver.Firefox()
driver.maximize_window()
url = r"https://www.nba.com/stats/players/traditional"
driver.get(url)
advanced = driver.find_element(By.XPATH, r'//div[2]/div[2]/div[3]/section[1]/div/nav/div[3]/ul/li[2]/a')
action = ActionChains(driver)
action.move_to_element(advanced)
advanced.click()
return error is always: element could not be scrolled into view.
I've tried other versions of this code including:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//div[2]/div[2]/div[3]/section[1]/div/nav/div[3]/ul/li[2]/a'))).click()
Please help, Thank you already
You need to accept the cookie consent button and then click on of toggle to expand the dropdown and then select the element.
driver.get("https://www.nba.com/stats/players/traditional")
#accept cookie consent
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button#onetrust-accept-btn-handler"))).click()
time.sleep(1)
#expand
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"(//div[#class='StatsQuickNavSelector_nav__JzoME']/button)[last()]"))).click()
#click on specific item
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Advanced']"))).click()
Try using:
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//div[2]/div[2]/div[3]/section[1]/div/nav/div[3]/ul/li[2]/a')))
driver.execute_script("arguments[0].scrollIntoView();", element)
element.click()
To scroll the element into view.
resource

Scrapping information from the new page loaded, after button is pressed using Python selenium

I am trying to extract few information from the page using selenium. Initially in the first page i extracted the name. And for few fields necessary input is given in default.
Later i pressed donate button in the bottom.
Now a new page is loaded. And i need to do certain operation here. But i could not do any operations in the new page loaded.
Below the code is given till donate button is clicked, followed by code for extracting information from new page.
url = "https://donations.iskconbangalore.org/mobile-donation/?patronId=13340"
driver = webdriver.Chrome(executable_path=r'C:\Users\Admin\Downloads\chromedriver_win32\chromedriver')
driver.get(url)
name=driver.find_element(By.ID,"donorName").get_attribute("value")
print(a)
mobile=driver.find_element(By.ID,"donorMobile").get_attribute("value")
print(mobile)
inputamt = driver.find_element(By.ID,"O5")
inputamt.send_keys('500')
inputemail = driver.find_element(By.ID,"donorEmailId")
inputemail.send_keys('me#me.me')
radioButtons = driver.find_element(By.CLASS_NAME, "custom-control-label").click()
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn order-btn2 donate-now']"))).click()
Now after the page is loaded i am trying to certain operations. But noting is happening. I wanted to scroll downn and click on the account button.
for information i have given id and class: id="account-tab-btn" class="account-section svelte-1mqsf83"
time.sleep(7)
driver.switch_to.window(driver.window_handles[0])
driver.get("https://donations.iskconbangalore.org/payment-gateway/")
# prints windows id
print(driver.window_handles)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='account-section svelte-1mqsf83']"))).click()
Payment-gateway page is inside an iframe, so you have to switch to that iframe, then you have to do the further actions:
After the below line:
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn order-btn2 donate-now']"))).click()
Add this line and try:
WebDriverWait(driver, 15).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, ".razorpay-checkout-frame"))) # switch to the payment-gateway iframe
time.sleep(1)
account_link = driver.find_element(By.XPATH, ".//*[starts-with(#class,'account-tab svelte-')]//div[#data-test-id='account-tab-btn']")
driver.execute_script("arguments[0].scrollIntoView(true)", account_link)
time.sleep(1)
account_link.click()
time.sleep(1)
driver.find_element(By.XPATH, ".//*[text()='Edit contact details']").click()

Element is found but not clickable

I'm trying to find an element by it's id, click on it and download a file.
driver.get(url);
driver.implicitly_wait(60);
time.sleep(3)
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "ContentPlaceHolder1_a1")))
href = element.get_attribute('href')
value = href.split('/')[-1]
print(value);
element.click(); # Error
Error
element click intercepted: Element is not clickable at point (110, 1003)
I've tried Xpath, and CSS path too. All give the same error. If I check for the visibility then it times out. But I can manually see that the element is visible
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(text(), 'text of the link')]")))
At last, I tried this code.
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "ContentPlaceHolder1_a1")))
ActionChains(driver).move_to_element(element).click().perform()
But it gives error
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: move target out of bounds
While identified the element, page is still loading, may be the reason it is clicking on somewhere else.
Scroll down to the element and then wait for 1 to 2 seconds and then click.
It is working perfectly for me.
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "ContentPlaceHolder1_a1")))
element.location_once_scrolled_into_view #scroll down to element
time.sleep(2) # wait for 2 seconds , 1 sec working as well
element.click()

Why would commands in selenium works seperately but if put in script selenium does'nt locate the elements

The problem i am facing is when i run the whole script it throws error of element not clickable or not found. While when i run it command per command it works.
If anyone can explain the reason and why it behaves this way i ll be very grateful.
codeExample:
driver.find_element(By.XPATH, "//div[#id=\'Content_C164_Col00\']/div/div/div[2]/div/div/div/div/div/button/span/span/span[3]").click()
driver.find_elements(By.CLASS_NAME, "fxs_c_datepicker_button")[1].click()
driver.find_element(By.CLASS_NAME, "fxs_btn.fxs_btn_cta.fxs_fRight").click()
ERROR:
ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (238, 772). Other element would receive the click: ...
(Session info: chrome=101.0.4951.64)
Stacktrace:
Backtrace:
I am working in VS code as my editor.
I reproduced your problem and had the same error. What i did to fix it is just scroll to the element before clicking it.
Try this out
driver.find_element(By.XPATH, "//div[#id=\'Content_C164_Col00\']/div/div/div[2]/div/div/div/div/div/button/span/span/span[3]").click()
driver.find_elements(By.CLASS_NAME, "fxs_c_datepicker_button")[1].click()
actions = ActionChains(driver)
button = driver.find_element(By.CLASS_NAME, "fxs_btn.fxs_btn_cta.fxs_fRight")
actions.move_to_element(button)
button.click()
I also noticed that after a few seconds a popup appears on the website. Make sure you click that one away as it may intercept the click on the apply button.
Update:
Here's the full code from opening the website to selecting date and clicking the apply button.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
# Opening browser and maximizing it
driver.get("https://www.fxstreet.com/economic-calendar")
driver.maximize_window()
# Click 'Continue to site'
driver.find_element(By.CLASS_NAME, "fxs_prestitial-continue").click()
# Wait until popup appears and cancel it
driver.implicitly_wait(10)
driver.find_element(By.XPATH, "//button[#id='onesignal-slidedown-cancel-button' and text()='Cancel']").click()
# Click the datepicker button and choose date
driver.find_element(By.XPATH, "//div[#id=\'Content_C164_Col00\']/div/div/div[2]/div/div/div/div/div/button/span/span/span[3]").click()
driver.find_elements(By.CLASS_NAME, "fxs_c_datepicker_button")[1].click()
# Creating actions instance
actions = ActionChains(driver)
# Scrolling to 'Apply' button and clicking it
button = driver.find_element(By.CLASS_NAME, "fxs_btn.fxs_btn_cta.fxs_fRight")
actions.move_to_element(button)
button.click()
I tested a new version that works:
WebDriverWait(driver, 120).until(EC.element_to_be_clickable((By.ID, "onesignal-slidedown-cancel-button"))).click()
while True:
try:
studio = driver.find_element(By.CLASS_NAME, "fxs_headline_tiny").text
# print(studio)
driver.execute_script("window.scrollTo(0, 450)")
driver.find_element(By.CLASS_NAME, "fxs_icon.fa-calendar-alt.fa-w-14").click()
time.sleep(0.5)
exact_date = driver.find_elements(By.CLASS_NAME, "fxs_c_datepicker_button")
for i in range(len(exact_date)):
exact_date_i = exact_date[i].text
if(exact_date_i == "Today"):
time.sleep(2)
exact_date[i].click()
break
break
except:
print("Studio not found")
driver.find_element(By.CLASS_NAME, "fxs_btn.fxs_btn_cta.fxs_fRight").click()

Can't click the button to view more results, getting exception element not clickable

I am trying to scrape some data from this site https://easy.co.il/list/Shopping, when you scroll down the list of businesses it has a button at the end to view more results, when I try to click it using .click function it raises an exception that element is not intractable, I have also tried using Keys.ENTER still same exception, I have tried waiting for the element to be clickable using this code.
results = driver.find_elements_by_xpath('//div[#class="biz-item "]')
print(len(results))
button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//button[#id="nextPageButton"]')))
the len(results) prints 25 which is all the businesses visible till the view more results button.
I have also tried locating the button, the element is visible on the page but just not clickable.
Can someone please look in to this? Thankyou!
You need to scroll the element before click.Use location_once_scrolled_into_view to scroll the element.
driver.get("https://easy.co.il/list/Shopping")
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[#id="nextPageButton"]')))
button.location_once_scrolled_into_view
button.click()
If you still gets the error then induce java scripts executor to click.
driver.get("https://easy.co.il/list/Shopping")
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[#id="nextPageButton"]')))
button.location_once_scrolled_into_view
driver.execute_script("arguments[0].click();", button)
Use .visibility_of_element_located method, then scroll:
button = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//button[#id="nextPageButton"]')))
button.location_once_scrolled_into_view
button.click()

Categories