How do I scroll up and then click with Selenium and python - python

I need to click on a button using selenium in python. This is what I have:
read_more_buttons = responses[0].find_elements_by_class_name("read-more")
if len(read_more_buttons) > 0:
read_more_buttons[0].click()
It works fine most of the time but sometimes there's an overlay on the bottom of the page, which can not be dismissed. Therefore I'd get this error:
[element] is not clickable at point (665.7333145141602,883.4666748046875) because another element <div class="raq-module js-raq-module"> obscures it
I tried to scroll down the page with this code right before calling click():
driver.execute_script("window.scrollTo(0, " + str(read_more_buttons[0].location["y"] + 120) + ")")
However, I'm still receiving the same error. It seems like by calling .click() the element is scrolled to the very bottom of the page, which is right under the overlay. How can I move up the page and then click?

Those dang overlays!
Here, let's try and use JS to scroll into view and then click:
read_more_buttons = responses[0].find_elements_by_class_name("read-more")
if len(read_more_buttons) > 0:
driver.execute_script("arguments[0].scrollIntoView(true);", read_more_buttons[0])
driver.execute_script("arguments[0].click()", read_more_buttons[0])

Related

How can I scroll a pop up element from Abnb with Selenium on Python?

hey guys I'm scrapping abnb with Selenium-Python. The issue is that I want to scrap the reviews from each abnb but in order to scrap them I need to scroll down the pop up element that appears.
As you can see on the image I click the button then the pop-up element appears and then I want to scroll but I can't. Of course as you can see all the reviews don't appear and I have to scroll if I want them to appear, they also don't appear on the DevTools from google chrome unless I scroll.
I tried:
reviews_lista = []
boton_revs = driver.find_element(By.XPATH, "//button[#data-testid='pdp-show-all-reviews-button']")
boton_revs.click()
I clicked on the button that you see on the image.
I selected the whole POP UP
bloque_pop = driver.find_element(By.XPATH, "//div[#class='_14vzertx']")
I tried to scroll with this code but it didn't work
scroll = 0
while scroll < 5: # scroll 5 times
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', bloque_pop)
time.sleep(1)
scroll += 1
I also tried
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
and this too:
bloque_pop = driver.find_element(By.XPATH, "//div[#class='_14vzertx']")
for i in range(5):
#driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", bloque_pop)
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)", bloque_pop)
time.sleep(1.5)
None of them work.
The easiest way to scroll inside a popup is to scrape elements contained in the popup and then scroll to the last element by using the javascript command scrollIntoView.
popup_reviews = driver.find_elements(By.CSS_SELECTOR, 'div[role=dialog] div[data-review-id]')
driver.execute_script('arguments[0].scrollIntoView({block: "center", behavior: "smooth"});', popup_reviews[-1])

How to click a button using selenium and draw on a canvas in python?

so I am trying to automate a program that would log in to a google account I created, go to this canvas website, and draw a circle,
(Circle just as a placeholder because im trying to make it draw some cool stuff like a car, just to test if it will work first.) But the main issue is when you first go into the website, there is a pop up that displays and that pop up has 2 options, "learn more" and "Get started". I tried to make selenium click on the "Get started" using driver.find_element_by_id('Get-started').click but it does not seem to work, I also tried to use CSS selector but that does not seem to work either. So, I'm stuck on this. Any advice or help to click the get started button? (Feel free to also give me advice on how to draw in the canvas as well!)
Here's the HTML:
<paper-button id="get-started" dialog-confirm="" class="primary" aria-label="Get started" role="button" tabindex="0" animated="" elevation="0" aria-disabled="false">
Get started
</paper-button>
here's the code:
from selenium import webdriver
import time
from PrivateInfo import*
driver = webdriver.Chrome(r"D:\chromeDriver\chromedriver.exe")
link = "https://www.google.com/"
driver.get(link)
def linkText(element):
button = driver.find_element_by_link_text(element)
button.click()
def byClass(className):
button2 = driver.find_element_by_class_name(className)
button2.click()
def type(text, elements):
input = driver.find_elements_by_name(elements)
for element in input:
driver.implicitly_wait(2)
element.send_keys(text)
linkText("Sign in")
type(email, "identifier")
byClass("VfPpkd-vQzf8d")
type(pw, "password")
driver.find_element_by_id("passwordNext").click()
time.sleep(1)
driver.get("https://canvas.apps.chrome/")
driver.implicitly_wait(3)
driver.find_element_by_css_selector('paper-button[id="get-started"]').click()
edit: I also tried this
getStart = driver.find_elements_by_css_selector('paper-button[id="get-started"]')
for start in getStart:
start.click()
it doesn't give me any errors but it does not do anything.
Ah yeah, forgot to mention but im new to using selenium.
The content of the popup is nested inside shadowRoots.
More on ShadowRoot here:
The ShadowRoot interface of the Shadow DOM API is the root node of a
DOM subtree that is rendered separately from a document's main DOM
tree.
To be able to control the element you will need to switch between DOMs. Here's how:
drawing_app_el = driver.execute_script("return arguments[0].shadowRoot", driver.find_element(By.CSS_SELECTOR, 'drawing-app'))
This code will retrieve the drawing_app first and then return the content of the shadowRoot.
To have access to the button getStarted, here's how:
# Get the shadowRoot of drawing-app
drawing_app_el = driver.execute_script("return arguments[0].shadowRoot", driver.find_element(By.CSS_SELECTOR, 'drawing-app'))
# Get the shadowRoot of welcome-dialog within drawing_app
welcome_dialog_el = driver.execute_script("return arguments[0].shadowRoot", drawing_app_el.find_element(By.CSS_SELECTOR, 'welcome-dialog'))
# Get the paper-dialog from welcome_dialog
paper_dialog_el = welcome_dialog_el.find_element(By.CSS_SELECTOR, 'paper-dialog')
# Finally, retrieve the getStarted button
get_started_button = paper_dialog_el.find_element(By.CSS_SELECTOR, '#get-started')
get_started_button.click()

I used selenium for scraping, but out of 56 listing i can get only 40 listings.but the class name is same for all the 56 listing

main url :https://www.kaplanpathways.com/degree-finder/#/search-result?status=7&institution_short_name=Arizona-State-University-Downtown-Phoenix-Campus&subject_area_name=&university=38&degree_level=20
try:
for i in range(1, 20):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
driver.find_element_by_xpath("/html/body/div[5]/main/div/div/app-root/app-search-result/div/div[2]/div[2]/div[2]/button[1]").click()
except:
pass
course_list = driver.find_elements_by_xpath("//*[#class='wrap-result']")
print("Total courses: ", len(course_list))
Problem:
When you are moving to the bottom of the page, you actually move out of the clickable area for the "Show more" button. selenium does not click the button if that is not clickable (i.e. out of the screen, or behind some div like accept cookies div in your case).
Solution:
Try clicking by injecting javascript.
driver.execute_script("""document.querySelector(".dgf-show-more-button").click()""")
Note: Also don't forgot to click the "I accept" button for cookies.

Is there a way to block/close iframes using seleium?

I am creating a downloader from Instagram. This program gets URLs from top Instagram posts in a given hashtag and inputs them into a downloader. The issue is that a popup iframe ad always appears over the download button when the website is first loaded. This throws an error that the button cannot be clicked because the iframe will be clicked.
This is for Python Selenium running Chrome driver. I have tried to run a filter that finds iframes and goes back to the main page:
all_iframes = self.browser.find_elements_by_tag_name("iframe")
if len(all_iframes) > 0:
self.browser.switch_to.default_content()
This did not work, I also tried to get the XPath to the X-button on the ad, but the ID changes every time, so I cannot be clicked or identified.
#get the website
for link in self.links:
self.browser.get('https://downloadgram.com/')
time.sleep(5)
#this is the x button click iframe I tried, but the XPath changes
all_iframes = self.browser.find_elements_by_tag_name("iframe")
if len(all_iframes) > 0:
xButton = self.browser.find_element_by_xpath('//div[#id="id3019a64023cross3019a64023"]')
xButton.click()
#inputs the URL from array links[] into download box
input = self.browser.find_element_by_xpath('//input[#name="url"]')
input.clear()
input.send_keys(link)
time.sleep(1)
#clicks download button
download = self.browser.find_element_by_xpath("//input[#type='submit']")
download.click()
time.sleep(1)
#clicks confirm button
actuallyDownload = self.browser.find_element_by_xpath("//a[#target='_blank']")
actuallyDownload.click()
time.sleep(1)
I expect the code to download the pictures at the url, but I get:
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <input type="submit" value="Download" class="button"> is not clickable at point (451, 446). Other element would receive the click:
the website (turn off adblock to see add) https://downloadgram.com/
I am able to close the pop up with following code.Please try that.
browser.get('https://downloadgram.com/')
time.sleep(5)
element=browser.find_element_by_xpath("//div[starts-with(#id,'id')]" and "//div[starts-with(#style,'position:absolute !important;height:20px !important;width:20px !important;top:3px !important;left:3px !important;background-image:url(data:image/png;')]")
arrt=element.get_attribute("id")
print(arrt)
browser.execute_script("arguments[0].click();", element)
Let me know if it works.Good Luck.

Element is not clickable (the button is blocking by other element)

I'm trying to click on this button: browser.find_element_by_id('btnSearch')
But this button is blocking by this div tag: <div id="actionSearch" class="row pull-right">
How do I go around to click this button with id='btnSearch" while it's blocking by the actionSearch div?
I tried the following:
browser.find_element_by_id('btnSearch').click()
browser.implicitly_wait(10)
el = browser.find_element_by_xpath('//*[#id="btnSearch"]')
ActionChains(browser).move_to_element_with_offset(el, 1827, 270)
ActionChains(browser).click()
ActionChains(browser).perform()
element = browser.find_element_by_id('btnSearch')
browser.execute_script("arguments[0].click();", element)
wait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, //*[#id="btnSearch"]'))).click()
none of these work.
Can anyone help me with this? I've spent two days trying to click this button!! Please help!
Considering provided image of HTML source (tip: do not provide it as image, but as text instead) I can assume that required element located in the bottom of page and you might need to scroll page down to be able to handle it.
Try below code
link = browser.find_element_by_id("btnSearch")
browser.execute_script("arguments[0].scrollIntoView()", link)
link.click()
Note that link is not a pseudo-element (is not located inside ::before/::after pseudo-element), so it can not be the cause of your problem
As for your code:
ActionChains(browser).move_to_element_with_offset(el, 1827, 270)
ActionChains(browser).click()
ActionChains(browser).perform()
Here you're trying to make scroll to link with huge offset and then you make click on current mouse position - not on link
You can try to modify it as
ActionChains(browser).move_to_element(el)
ActionChains(browser).click(el) # Pass WebElement you want to click as argument to `click()`
ActionChains(browser).perform()

Categories

Resources