Can't scroll down in web page - python

In python and selenium I can't scroll down in this page https://melkemun.com/ to show & get another post
driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")

This page uses body to crop displayed area and real scrolled area is
<div class="scroll-body" onscroll="loadMore(event)">
This works for me in JavaScript console (in DevTools in Firefox)
item = document.getElementsByClassName('scroll-body')[0];
item.scrollTo(0, item.scrollHeight);
so you may need
driver.execute_script("item=document.getElementsByClassName('scroll-body')[0];item.scrollTo(0,item.scrollHeight);")
Page uses Shadow Root and code works only with Chrome. (Selenium: 4.4.0)
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
#from webdriver_manager.firefox import GeckoDriverManager
import time
url = 'https://melkemun.com/'
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
#driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))
driver.get(url)
print('sleep')
time.sleep(5)
shadow = driver.find_element(By.ID, 'stting').shadow_root
#print(shadow)
# select city
shadow.find_elements(By.CLASS_NAME, "btn-city")[0].click()
print('sleep')
time.sleep(5)
for i in range(5):
print('scrolling:', i)
#driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
driver.execute_script('item=document.getElementsByClassName("scroll-body")[0];item.scrollTo(0,item.scrollHeight);')
print('sleep')
time.sleep(5)

Related

Selenium :Cant find and click element by X-path

I am trying to click a button on website using selenium. But selenium cant find it. It is not iframe or problem with X-path. What I think is there is some event to make the element available by javascript. I tried to get it using class although it had very bad class value. If any can help. It will be a relief for me. I tried my best. I got the X-path from firefox developer tools.The url of site is :https://www.bedbathandbeyond.com/store/category/kitchen/trash-recycling/14367
I want to click the "next" button (available almost in the bottom)
from selenium import webdriver
from selenium.webdriver.chrome import service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
option = webdriver.ChromeOptions()
option.add_argument("start-maximized")
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_experimental_option('useAutomationExtension', False)
option.add_argument("--disable-blink-features")
option.add_argument("--disable-gpu")
#option.add_argument("--headless")
option.add_argument('--disable-blink-features=AutomationControlled')
wd = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=option)
wait = WebDriverWait(wd, 40)
wd.get('https://www.bedbathandbeyond.com/store/category/kitchen/trash-recycling/14367')
wait.until(EC.element_to_be_clickable((By.XPATH, "/body/div[3]/div[8]/div[1]/div[3]/div[2]/amp-list/div/div/button[2]")))
wd.find_element(By.XPATH,'/body/div[3]/div[8]/div[1]/div[3]/div[2]/amp-list/div/div/button[2]').click()
# class="plpPage plpNext flex mid pwaOnly "
time.sleep(15)
wd.quit()
EDIT:
I think it is some shadow root. I dont know much but tried this code but it did not worked
def expand_shadow_element(element):
shadow_root = wd.execute_script('return arguments[0].shadowRoot', element)
return shadow_root
wd.get('https://www.bedbathandbeyond.com/store/category/kitchen/trash-recycling/14367')
wait.until(EC.element_to_be_clickable((By.XPATH, '/body/div[3]/div[8]/div[1]/div[3]/div[2]/amp-list/div/div')))
ele = wd.find_element(By.XPATH, '/body/div[3]/div[8]/div[1]/div[3]/div[2]/amp-list/div/div')
root = expand_shadow_element(ele)
It gives Timeout error on the wait
you could use javascript instead..this will click the next button
driver.execute_script(
'document.querySelector("#wmHostPrimary").shadowRoot.querySelector("button.plpPage.plpNext.flex.mid.pwaOnly").click()')

Selenium bot for ServiceNow

I am trying to create bot for ServiceNow. But Somehow Selenium is not able to see Javascript elements. I use method find_element_by_xpath. But element is not visible, like it is not render. I print page by .page_source. Javascript is not there. How to run Javascript to be visible by Selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
import time
PATH = ".\\chromedriver.exe"
driver = webdriver.Chrome(PATH)
chrome_options = Options()
chrome_options.headless = True
chrome_options.add_argument("--enable-javascript")
driver = webdriver.Chrome(options=chrome_options)
driver = webdriver.PhantomJS()
driver.get(https://cannotsharepage.com/)
# driver.maximize_window()
sso = driver.find_element_by_xpath("//*[#id='bySelection']/div[2]").click()
searchClick = driver.find_element_by_xpath("/html/body/div[5]/div/div/header/div[1]/div/div[2]/div/div[4]/form/div/label/span").click()
ritNumber="RITM0179411"
searchText = driver.find_element_by_xpath("//*[#id='sysparm_search']").send_keys(ritNumber)
time.sleep(3)
SearchText =driver.find_element_by_xpath("//*[#id='sysparm_search']").send_keys(Keys.ENTER)
time.sleep(15)
approvers= driver.execute_script("//*[#id='tabs2_list']/span[3]/span")
approvers = driver.page_source

youtube page scroll down using selenium, python

url_main = "https://www.youtube.com/feed/history"
I want to scroll down infinitely in the webpage above until all contents are visible, so I tried
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import time
driver = webdriver.Chrome(driverpath)
wait = WebDriverWait(driver, 10)
driver.get(url_main)
login = driver.find_element_by_css_selector('a[href="https://accounts.google.com/ServiceLogin?service=youtube&uilel=3&passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26app%3Ddesktop%26hl%3Dko%26next%3Dhttps%253A%252F%252Fwww.youtube.com%252Ffeed%252Fhistory&hl=ko&ec=65620"]')
login.click()
login = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[type="email"]')))
login.send_keys(email)
login.send_keys(Keys.RETURN)
login = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[type="password"]')))
login.send_keys(password)
login.send_keys(Keys.RETURN)
content = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'div[id="primary"] div[id="contents"]')))
no_of_pagedowns = 1
while no_of_pagedowns:
content.send_keys(Keys.PAGE_DOWN)
time.sleep(1)
no_of_pagedowns-=1
But not working. I also tried copy-pasting the code in this link but it didn't work at all also.
You can implement something like this:
# save the max scrollHeight of the page
scrollHeight = d.execute_script("return window.scrollMaxY")
scrolled_pages = 0
# while we have not reached the max scrollHeight
while d.execute_script("return window.pageYOffset") < scrollHeight:
# scroll one page down
d.execute_script("window.scrollByPages(1)")
scrolled_pages += 1
# wait to load any lazy loaded images (may not be needed, depending on your usecase)
time.sleep(0.2)

How to use click in Selenium for opening links

I am having trouble with Selenium lately.
I'm trying to get all the URL from
http://cimex.co/resources.html
and open it on a new tab.
I'm trying to achieve with this code:
import selenium, os
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://cimex.co/resources.html')
links = browser.find_elements_by_css_selector('a[href^="https"]')
links[0].click()
While I am facing another problem is there any chance if it detects Firefox running just open the URL in a new tab rather than opening Firefox as a new app.
Thanks.
Sorry for my bad English.
I already had to do that, it was with the ChromeDriver, I cannot say if it will works the same with Firefox, but here is the code:
def Browser(object):
def __init__(self):
self.driver = webdriver.Chrome(driver_path)
self._tabs = {'default': self.driver.window_handles[0]}
def new_tab(self, name):
'''
Create new tab `name`.
name New tab name
'''
self.driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
self._tabs[name] = self.driver.window_handles[-1]
def switch_tab(self, name):
'''
Switch to given tab.
name Tab name to switch
'''
self.driver.switch_to_window(self._tabs[name])
browser = Browser()
browser.driver.get('http://cimex.co/resources.html')
links = browser.driver.find_elements_by_css_selector('a[href^="https"]')
url = links[0].get_attribute('href')
# open new tab
browser.new_tab(url, 'tab-name')
# switch to new tab
browser.switch_tab('tab-name')
# back to default tab
browser.switch_tab('default')
You can try this code :
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
browser = webdriver.Firefox(executable_path = r'D:/Automation/geckodriver.exe')
browser.get("http://cimex.co/resources.html")
wait = WebDriverWait(browser, 30)
main_tab = browser.current_window_handle
print(main_tab)
links = browser.find_elements_by_tag_name('a')
size = len(links)
print ('this the length of list:',size)
i = 0
while i<size:
ActionChains(browser).key_down(Keys.CONTROL).click(links[i]).key_up(Keys.CONTROL).perform()
browser.switch_to_window(main_tab)
i=i+1;
if i >= size:
break
print(" here you quit the driver as per your wish")
from selenium import webdriver
browser = webdriver.Firefox(gecko driver location)
browser.get('http://cimex.co/resources.html')
links = browser.find_elements_by_css_selector('a[href^="https"]')
for link in links:
link.click()

Use Selenium to click javascript image in Python

I am trying to use Selenium to click on a javascript image on a webpage, but can't figure out how to do it.
The code I have so far is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.get("http://explorer.natureserve.org/servlet/NatureServe?init=Ecol")
assert "NatureServe" in driver.title
#elem = driver.find_element_by_name("")
SciName = driver.find_element_by_name('nameSpec')
SciName.send_keys(names)
mouse = webdriver.ActionChains(driver)
element = driver.find_element_by_name('nameCriteriaForm')
mouse.move_to_element(span_element).click().perform()
The last two lines are clearly wrong. What I want to select is located in the page source as:
<img src=" http://explorer.natureserve.org/images/search_now.gif" width="77" height="17" border="0" align="absmiddle">
It is located multiple times on the page and links to a gif.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
driver.get("http://explorer.natureserve.org/servlet/NatureServe?init=Ecol")
SciName = driver.find_element_by_name('nameSpec')
SciName.send_keys(names)
SciName.submit()

Categories