I'm trying to scroll at the bottom of the page. I was adviced, here on SO, to do this:
from selenium.webdriver.common.keys import Keys
element = driver.find_element_by_ ...
element.send_keys(Keys.CONTROL , Keys.END)
I can't figure out what element shoul I use. I was trying to put a webdriver instance instead of element but it did not work. I need something like current window element?
Have you any ideas?
This should be enough to make scroll to page bottom
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver=webdriver.Chrome()
driver.get("site_name")
driver.find_element_by_xpath('//body').send_keys(Keys.CONTROL+Keys.END)
A simple javascript should be sufficient as well. Python syntax
driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
body = driver.find_element_by_xpath('/html/body')
body.click()
ActionChains(driver).key_down(Keys.COMMAND).send_keys(Keys.ARROW_DOWN).perform()
Looking at your previous question Link This works on mac. Change the combo for Windows.
Related
I have tried driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") after the page has loaded to no avail. It simply does nothing.
Why isn't it working? Is there another method I can use.
scrollTo is usually the preferred way but not possible on every site.
Alternatively you can use this:
from selenium.webdriver.common.keys import Keys
elem = driver.find_element(By.TAG_NAME, "html")
elem.send_keys(Keys.END)
However, I would much prefer requests instead of selenium.
There are several ways to scroll the page with Selenium.
Additionally to
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
You can try
from selenium.webdriver.common.keys import Keys
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.END)
But the most powerful way that normally works even when the previously methods will not is:
Locate some element out of the visible screen, maybe on the bottom of the page and apply on it the following
bottom_element = driver.find_element(By.XPATH, bottom_element_locator)
bottom_element.location_once_scrolled_into_view
This originally intend to return you coordinates (x, y) of element on page, but also scroll down right to target element
Another way to scroll to bottom of page is to emulate the CTRL + END key combination
from selenium.webdriver.common.keys import Keys
driver.find_element_by_css_selector('body').send_keys(Keys.CONTROL + Keys.END)
I want help in a little thing,
Look this:
I want to press the option named Boleto Bancario, but look the html
Than how I will press the second option with selenium PYTHON
Please Check the snippet.
You can select by value
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome(r'chromedriver.exe')
driver.get('url')
sct = Select(driver.find_element_by_id('idFormaPagamento'))
sct.select_by_value('813640')
You can select by index
sct.select_by_index('1')
You can select the value in any dropdown by three different ways:
select_by_value()
select_by_index()
select_by_visible_text()
So you may simply go and choose the option like this:
select_by_value('813640')
select_by_index('1')
select_by_visible_text('Boleto Bancario')
Hope this works for you.
I'm trying to use Selenium to create a new message in my mailbox. I have a problem with finding napisz (en: 'write') button on my e-mail website. I tried to use driver.find_element_by_link_text but it doesn't work. I've managed to go workaround this problem using xpath but I'm very curious why the first method fails.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
browser.get('https://profil.wp.pl/login.html?zaloguj=poczta&url=https://poczta.wp.pl/profil/')
elem_login = browser.find_element_by_name('login_username')
elem_login.send_keys('stack_scraper_wp#wp.pl')
elem_password = browser.find_element_by_name('password')
elem_password.send_keys('thankyouforhelp')
elem_zaloguj_button = browser.find_element_by_id('btnSubmit')
elem_zaloguj_button.click()
browser.get('https://poczta.wp.pl/d635/indexgwt.html#start')
elem_napisz_button = browser.find_element_by_link_text('napisz')
elem_napisz_button.click()
EDIT: I've tried to used same xpath today but it failed. Is it possible that it's somehow dynamic causing the problem?
.find_element_by_link_text() looks for a elements only. In your case, this is the button element and cannot be located using this locator.
I'm trying to use Pythons Selenium module to click on an element whose link has the text "xlsx" at the end of it. Below is the code I'm using and the details of the element. Can someone please see why Python is unable to find this element?
driver.find_element_by_partial_link_text('xlsx').click()
Here is the element details:
<a name="URL$2" id="URL$2" ptlinktgt="pt_new" tabindex="43" onfocus="doFocus_win0(this,false,true);" href="http:******/HSC8_CNTRCT_ITEMS_IMPRVD-1479218.xlsx" onclick="window.open('http:********/HSC8_CNTRCT_ITEMS_IMPRVD-1479218.xlsx','','');cancelBubble(event);return false;" class="PSHYPERLINK">HSC8_CNTRCT_ITEMS_IMPRVD-1479218.xlsx</a>
I had to remove some parts of the URL for confidentiality purposes, however, it should not impact the answering of the question.
Thanks.
Thanks for the replies. Turns out, as #Andersson mentioned, the window was in a different frame.
I solved the problem using the following code before the find_element: driver.switch_to.frame('ptModFrame_0').
You can use a CSS selector:
driver.find_element_by_css_selector("a[href*='xlsx']")
If the element still cannot be located, I would suggest using a wait statement, to ensure that the element is visible, before you interact with it.
Please try:
driver.find_element_by_xpath(".//a[contains(#href,'xlsx')]").
You can grab it by class name (class name = PSHYPERLINK).
This should work:
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
url = ''
driver = webdriver.Chrome('/path/to/chromedriver/executable')
if __name__=='__main__':
driver.get(url)
time.sleep(3)
driver.find_element_by_class_name('PSHYPERLINK').click()
When finding the attribute, make sure to use a singular '"element". Like:
driver.find_element_by_class_name('PSHYPERLINK').click()
not:
driver.find_elements_by_class_name('PSHYPERLINK').click()
Hope this helps.
I would like to jump through form elements, the same way you do when you hit tab. I can't find a webdriver way to do this yet. This is for when I know the order of the form and don't need to worry about ids or names to find the element. Thank you
You can iterate through list of input fields using Keys.TAB:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver=webdriver.Firefox()
list = driver.find_elements_by_tag_name('input')
for input_field in list:
input_field.send_keys("enter some text here")
input_field.send_keys(Keys.TAB)