selenium to scroll to top of page [duplicate] - python

This question already has answers here:
Scrolling to top of the page in Python using Selenium
(8 answers)
What is the difference between the different scroll options?
(2 answers)
Closed 3 years ago.
I am trying to scroll Up in a whats app conversation so I record the entire chat (into csv).
chrome_options = Options()
chrome_options.add_argument("--user-data-dir=chrome-data") #saves login
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://web.whatsapp.com/')
#choose converstaion
#scroll to top not working
Here is what I tried but it didn't work, Ideally I will run headless so I prefer not to use pyautogui
def scroll_to_top():
driver.execute_script("window.scrollTo(0, -document.body.scrollHeight);")
time.sleep(4)
#driver.execute_script("window.scrollTo(0, 0);")
#bd = driver.find_element_by_xpath('//body')
#bd.click()
#bd.send_keys(Keys.CONTROL+Keys.HOME) #throws element not interact-able
Any help is appreciated

Please use below code to scroll to top of your page
solution 1 :
driver.execute_script("window.scrollTo(0, 220)")
solution 2 :
from selenium.webdriver.common.keys import Keys
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.PAGE_UP)

Related

how to scrape a dynamic website without selenium [duplicate]

This question already has answers here:
Can a website detect when you are using Selenium with chromedriver?
(25 answers)
How to find the $cdc_ (chromedriver params) in website?
(1 answer)
Selenium Webdriver can't access a website (The requested URL was rejected)
(1 answer)
Closed 2 years ago.
i am facing an issue that i hope anyone can help me with.
i'm trying to scrape a webpage using selenium package on python but it keeps detecting that i'm using selenium and redirects me to a log in page that i can't use.
i tried using a fake agent but it still detected me, i tried changing the "$cdc" in the chrome driver but it still didn't work. apperciate if anyone can help me.
here is the code i'm using :
options = webdriver.ChromeOptions()
ua = UserAgent()
userAgent = ua.random
options.add_argument('--ignore-certificate-errors')
options.add_argument('--incognito')
options.add_argument("--window-size=1920,1080")
options.add_argument('enable-automation')
options.add_argument(f'user-agent={userAgent}')
#options.add_argument('--headless')
driver = webdriver.Chrome()
driver.get("https://www.mcmaster.com/nuts/hex-nuts/medium-strength-steel-hex-nuts-grade-5/")

Web automation error in python while running..i m using selenium [duplicate]

This question already has answers here:
ElementNotVisibleException: Message: element not interactable error while trying to click a button through Selenium and Python
(2 answers)
Closed 2 years ago.
I just started web automation and I can't figure out this error..
Code-
from selenium import webdriver
site = webdriver.Chrome()
site.get('https://www.youtube.com')
searchbar = site.find_element_by_xpath('//*[#id="search"]')
searchbar.click()
searchbar.send_keys('Fireproof')
searchButton = site.find_element_by_xpath('//*[#id="search-icon-legacy"]')
searchButton.click()
Error in cmd-
Its opening the browser but not doing anything after that
Issue is with XPath, user below code having updated XPath.
searchbar = driver.find_element_by_xpath('//input[#id="search"]')
searchButton = driver.find_element_by_xpath('//button[#id="search-icon-legacy"]')

Python Selenium error Element is not clickable at point [duplicate]

This question already has answers here:
Element MyElement is not clickable at point (x, y)... Other element would receive the click
(5 answers)
Closed 3 years ago.
I am trying to scrape this website with selenium on python I keep getting this error every time I try to click on a dropdown button that is inside a div tag can some help me please the error is 'Element is not clickable at point (1341, 240)' below is the website
'https://tennisinsight.com/player/56330/andrea-gamiz/'
if you scroll to the bottom of the page, I am trying to click on the duration dropdown options in the match stats sections. below is my code so far
driver = webdriver.Chrome()
driver.maximize_window()
wait = WebDriverWait(driver, 5)
small_wait = WebDriverWait(driver, 5)
driver.execute_script('window.open("https://tennisinsight.com/player/56330/andrea-gamiz/","_self")')
driver.execute_script("document.body.style.zoom='75%'")
from selenium.webdriver.common.keys import Keys
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.END)
time.sleep(3)
element = wait.until(EC.element_to_be_clickable((By.XPATH, ' //*[#id="matchStatsDuration"]')))
element.click()
Here is the simple approach that I would follow to select the items from this list.
# select Month from the list.
element = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//select[#id='matchStatsDuration']/option[.='Month']")))
element.location_once_scrolled_into_view
element.click()
By this way I don't have to worry about the overlaying top menu, which is obstructing the click on the list element.

Selenium click not working in Chrome [duplicate]

This question already has answers here:
Filling in login forms in Instagram using selenium and webdriver (chrome) python OSX
(9 answers)
Closed 4 years ago.
I am trying to click on the anchor tag on instagram's page which says log in. Here is my code.
browser = webdriver.Chrome()
browser.get('https://instagram.com')
login_elem = browser.findElement(By.xpath('//p/a[text() = "Log in"'))
login_elem.click()
The browser opens however the element is not clicked. I have tried various other xpaths and none worked. Here is the image for the Instagram source.
This is what i tried on my local system and it worked
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome(executable_path="D:\\cs\\chromedriver.exe")
driver.get("https://www.instagram.com/")
a=driver.find_element(By.XPATH,'//a[text() = "Log in"]')
# added this step for compatibility of scrolling to the view
driver.execute_script("return arguments[0].scrollIntoView();", a)
a.click()
Corrected the XPATH and other changes.Please note executable path should be replaced with your path.
You can use below Xpath
.//*[#id='react-root']/section/main/article/div[2]/div[2]/p/a
or CSS Selector
.izU2O>a
or Link Text
Log in
try this code :
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Log in")))
element.click()

Selenium / Python switch webdriver from headless to window mode [duplicate]

This question already has answers here:
How do I make Chrome Headless after I login manually
(2 answers)
Closed 4 years ago.
Is there any way to switch Chrome webdriver from headless mode to window mode?
One thing that came to my mind is to 'switch' existing web driver to non-headless mode.
Another idea: to create new instance of webdriver (this time non-headless) with some sort of 'state' from old one so the user operations can be executed. I don't know how to do or if it is possible though.
import os
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException,
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(
executable_path=os.path.join(os.getcwd(), 'chromedriver'),
chrome_options=options,
)
driver.get('https://website.com')
try:
driver.find_element_by_xpath('//h1').click()
except NoSuchElementException:
print('You have to click it manually')
# here I need Chrome browser
# to be opened so that I can click a link
print('The name of this thing is: ', end='')
print(driver.find_element_by_xpath("//h1[#class='name']").text)
If you need to open a new tab
driver.execute_script("window.open()")
If you need to switch to this new one
driver.switch_to.window(self.driver.window_handles[1])
Then you get the page
driver.get('https://website.com')
and the end you can close it (the new one)
driver.close()
and you back to the first driver
switch_to.window(driver.window_handles[0])

Categories