Python Selenium Loop click through links - python

I am unable to loop click the links. When I try loop click the links it keeps clicking the first link only.
From the html code, I need the element named "key" value as well. How to capture it.
html file copy in dropbox. Please click https://www.dropbox.com/sh/85rx13m8iqwax4b/AACNDq_YyOukLh22JNv76vjua?dl=0.
html code
https://pastebin.com/Cyg98W2C
Python code I tried
elem = WebDriverWait(browser, 200).until(EC.element_to_be_clickable((By.XPATH, "//DIV[#id='propertySummaryList']/DIV[#class='summaryListItem ']/DIV[1]/DIV[3]/DIV[1]/H2[1]/A[1]")))
elem.click()
browser.back()
Edit: Added dropbox link. Since the site is sign in only. I have made a copy of the page.

You can gather all the elements, then use a relative find to find the link you need. Be careful, this may cause stale elements if you don't open the click in a new window.
summaryList = driver.find_elements_by_xpath("//DIV[#id='propertySummaryList']/DIV[#class='summaryListItem ']")
for elements in summaryList:
link = elements.find_elements_by_xpath(".//h2//a")
link.text // or link.click() but need to open in a new window or will get staleElementReference

Related

Changing focus to a new window in Python/Selenium

Just like the title says, I am having trouble with getting my code to focus on a new window, using the "driver.switch_to_window".
My task is to:
Click button on the parent page > new window appears > click an "accept" button on the new window and continue with the rest of the code
driver.get("https:testpage.com/")
driver.find_element_by_xpath("/html/body/div[1]/div[1]/main/div/div/div/div[2]/ul/li[1]/button").click()
#here the window appears
time.sleep(2)
driver.switch_to_window("windowName")
driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[3]/button[2]").click() #here nothing happens
You can try to get the page source so that you are sure whether the driver has switched or not:
html_source = browser.page_source
It is possible that it has switched, but your element is not loaded or is in iframe. If your element is present, then you can try with different XPath that is relative, e.g. find the nearest id and find your element from it:
//div[#id='someid']//button[text()='someText']
I do not use absolute XPaths as I think they are too fragile.
Reference:
Python Selenium accessing HTML source
What is the difference between absolute and relative xpaths? Which is preferred in Selenium automation testing?

Scroll on a specific DIV Element in Python Selenium

I'm trying to do a simple Python Selenium automation where the script will click a link which opens a dialog box on top of the page (Instagram profile).
The said dialog box will display the list of followers but unfortunately the UL that contains the list will only display the first 12 followers (or LI). It is powered by AJAX to "load more" followers.
Anyway, to simulate loading more followers, i tried this code:
driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/ul').send_keys(Keys.END)
or
driver.find_element_by_tag_name('body').send_keys(Keys.END)
unfortunately, it doesn't work. I was wondering if there is a correct way to do this (scrolling down focused on the active div or any page element)?
Image below is provided to show the html structure of the said page.
Appreciate your help on this, thank you very much!
Can you try something like this?. This will scroll to the div element that you have mentioned.
Python Code
element = driver.find_element_by_xpath("xpath_of_div_element")
driver.execute_script("arguments[0].scrollIntoView(true);", element);
Java sample:
WebElement element = driver.findElement(By.id("id_of_div_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Use this
liElement = driver.find_element_by_xpath("//div[#role='dialog']//ul//li[text()='required_li_element_visible_text']")
driver.execute_script("arguments[0].scrollIntoView(true);", liElement);

an element is not visually searchable in chrome inspection

I am building a web scraper selenium but the element I want to click is actually searched in chrome inspection layout but can't be seen visually(like when you hit ctrl+F and search for an element by typing it in, it shows up in the DOM structure collapsing...)
So I can't proceed since I should make a "clicking" on it with my Python.
http://bitly.kr/Nypl88
is the link and the element I would like to click with selenium has an id called "cns_Tab21". When you search for it, the result total is 1 but can't be seen in the DOM.
Thank you for reading this post and for your answer in advance.
Your element is hidden inside an iframe.
To get to it you will need to switch to the iframe first:
driver.get("https://finance.naver.com/item/coinfo.nhn?code=255440");
WebElement iframe = driver.findElement(By.id("coinfo_cp"));
driver.switchTo().frame(iframe);
WebElement tabElement = driver.findElement(By.id("cns_Tab21"));
*Edit*
Added a Python implementation
driver = webdriver.Chrome()
driver.get("https://finance.naver.com/item/coinfo.nhn?code=255440")
iframe = driver.find_element_by_id("coinfo_cp")
driver.switch_to.frame(iframe)
tabElement = driver.find_element_by_id("cns_Tab21")

How to do a loop on a dynamic href link with selenium in python?

I would like to make a loop on a dynamic href. Indeed, I download a set of files per page. On each page, I download 100 text files but I have to download 200 000 files. So, I have to click the next button in 2000. To do this, I got the href address of the next button but unfortunately, two objects change in this link, the page number 1,2,3, etc. and a string of characters. Please see attached sample of the next button that changes.
https://search.proquest.com/something/E6981FD6D11F45E8PQ/2?accountid=12543#scrollTo
https://search.proquest.com/something/E6981FD6D11F45E8PQ/3?accountid=12543#scrollTo
https://search.proquest.com/something/61C27022597C4092PQ/4?accountid=12543#scrollTo
https://search.proquest.com/something/E431552DC6554BF7PQ/5?accountid=12543#scrollTo
I'm novel user of Python. My level is bad.
#Before I add selenium setup for scraping.
n=2000
for i in range(1,n):
href="https://search.proquest.com/something/715376F5A5AF44BBPQ/" + str(i) + "?accountid=12543#scrollTo"
driver.get(href)
#Here, I add the code which allows downloading for each page.
Sample link is unavailable for me (i cannot signing up)
First..
what is "string of chacracters"?
book number? or category number?
if it is just random string, i think you should find another way.
How about using ActionChain? or driver.execute_script()?
First of all, In my opinion, Finding a meaning of string (from .js or .html) is more important.
#나민오 I need help in identifying xpath for my next page button. My goal consists to loop through pages in Python Selenium. Please find below the code of the next page button after inspecting on URL page on this picture.
next page button picture after inspect
I try to write the following code in python with selenium to download the file by page.
while True:
scraping() # here I call my function that allows to download the files per page
try:
#Checks if there are more pages with links
next_link = driver.find_element_by_xpath("//*[#title='Page suivante']")
drive.execute_script("arguments[0].scrollIntoView();", next_link)
next_link.click()
#Time sleep
time.sleep(20)
except NoSuchElementException:
pages_rows= False

Selenium Webdriver loop over iframe

I am trying to webscrape an iframe object containing a list of links. The ultimate objective is to perform a series of actions, same for each link, that leads to downloading some data in xls format.
For each of the links in the iframe, I want to automate the following actions:
click on the link
select from a drop down list the option for getting data since 1999
click on the excel download button.
My code works for the first of the links, but I am struggling with automating it to go through all the links in the iframe.
This is the code that works for the first link:
#this automates the log in
driver.find_element_by_id('user').send_keys('myusername')
driver.find_element_by_id('password').send_keys('mypassword')
driver.find_element_by_xpath('myxpath').click()
#this switches to iframe and clicks on all the links
driver.switch_to.frame("report-iframe")
driver.find_element_by_tag_name('a').click()
#this performs the required actions for the first link(select drop down, select excel download)
s= Select(driver.find_element_by_id('start-date'))
s.select_by_value('1999-01-01')
driver.find_element_by_xpath('//*[#id="report-excel-download"]')
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'report-excel-download')))
element.click()
I want to automate this process for all the links in the iframe (i.e. for each of the links in iframe, click on the link, select the drop down, click the data download button. Does anyone have any advice in how to do this?
I tried:
links=[link.get_attribute('href') for link in driver.find_elements_by_xpath('myxpath')]
for l in links:
#click the link
driver.find_element_by_tag_name('a').click()
#select all years
s= Select(driver.find_element_by_id('report-control-date-start'))
s.select_by_value('1999-01-01')
#click download
driver.find_element_by_xpath('//*[#id="report-excel-download"]')
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'report-excel-download')))
element.click()
But this only works for the first link in the iframe, it does not work across all.
Does anyone have any advice?
Thanks!

Categories

Resources