Click same id buttom selenium PYTHON - python-3.x

from selenium import webdriver
import time
username = ""
password = ""
getdriver = ("https://www.instagram.com/accounts/login/")
driver = webdriver.Chrome()
driver.get(getdriver)
driver.find_element_by_xpath("//input[#name='username']").send_keys(username)
driver.find_element_by_xpath("//input[#name='password']").send_keys(password)
driver.find_element_by_xpath("//button[contains(.,'Log in')]").click()
time.sleep(5)
driver.get("https://www.instagram.com/python.coders/followers/")
driver.find_element_by_xpath('//*[#id="react-root"]/section/main/div/header/section/ul/li[2]/a').click()
while True:
time.sleep(2)
driver.find_element_by_name('Follow').click()
I want to click all Follow display buttom on instagram,i can not do it with tag_name or name.

Use findElements.
It returns a list of WebElements with same locator.
Now loop around the size of this list and keep on clicking the WebElements.

Related

Switching to pop-up window that cannot be found via windows_handle Selenium/Pytohn

I dug up my old code, used for Scopus scraping. It was created while I was learning programming. Now a window pops up on the Scopus site that I can't detect using windows_handle.
Scopus Pop-up window
import openpyxl
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
import time
import pandas as pd
from openpyxl import load_workbook
DOI = []
TITLE = []
NUM_AUTHORS = []
NUM_AFFILIATIONS = []
AUTHORS = []
YEAR = []
JOURNAL = []
DOCUMENT_TYPE = []
COUNTRIES = []
SEARCH = []
DATES = []
chrome_driver_path = "C:\Development\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.get("https://www.scopus.com/search/form.uri?display=basic#basic")
# searching details
search = input("Search documents: \n")
SEARCH.append(search)
date = input("Do you want to specify dates?(Yes/No)")
if date.capitalize() == "Yes":
driver.find_element(By.CLASS_NAME, 'flex-grow-1').send_keys(search)
driver.find_element(By.XPATH,
"/html/body/div/div/div[1]/div[2]/div/div[3]/div/div[2]/div["
"2]/micro-ui/scopus-homepage/div/div/els-tab/els-tab-panel[1]/div/form/div[2]/div[1]/button["
"2]/span[2]").click()
time.sleep(1)
starting_date = input("Put starting year.")
to_date = input("Put end date.")
DATES.append(starting_date)
DATES.append(to_date)
drop_menu_from = Select(driver.find_element(By.XPATH,
"/html/body/div/div/div[1]/div[2]/div/div[3]/div/div[2]/div["
"2]/micro-ui/scopus-homepage/div/div/els-tab/els-tab-panel["
"1]/div/form/div[2]/div[1]/els-select/div/label/select"))
drop_menu_from.select_by_visible_text(starting_date)
drop_menu_to = Select(driver.find_element(By.XPATH,
"/html/body/div/div/div[1]/div[2]/div/div[3]/div/div[2]/div["
"2]/micro-ui/scopus-homepage/div/div/els-tab/els-tab-panel["
"1]/div/form/div[2]/div[2]/els-select/div/label/select"))
drop_menu_to.select_by_visible_text(to_date)
driver.find_element(By.XPATH,
'/html/body/div/div/div[1]/div[2]/div/div[3]/div/div[2]/div['
'2]/micro-ui/scopus-homepage/div/div/els-tab/els-tab-panel[1]/div/form/div[4]/div['
'2]/button/span[1]').click()
else:
DATES = ["XXX", "YYY"]
driver.find_element(By.CLASS_NAME, 'flex-grow-1').send_keys(search)
driver.find_element(By.XPATH,
"/html/body/div/div/div[1]/div[2]/div/div[3]/div/div[2]/div["
"2]/micro-ui/scopus-homepage/div/div/els-tab/els-tab-panel[1]/div/form/div[2]/div["
"2]/button").click()
time.sleep(2)
doc_num = int(driver.find_element(By.XPATH,
"/html/body/div[1]/div/div[1]/div/div/div[3]/form/div[1]/div/header/h1/span[1]").text.replace(
",", ""))
time.sleep(5)
driver.find_element((By.XPATH, "/html/body/div[11]/div[2]/div[1]/div[4]/div/div[2]/button")).click()
This is how the beginning of the code looks like. The last element
driver.find_element((By.XPATH, "/html/body/div[11]/div[2]/div[1]/div[4]/div/div[2]/button")).click()
should find on click on dismiss button. I do not know how to handle it.
I have tried finding element by driver.find_element, checking if the pop-up window can be detected and handled via windows_handle.
Actually that is not a popup because its code is contained in HTML of the page itself. Popups are either prompts of the browser (not contained in the HTML) or other browser windows (have a separate HTML).
I suggest to target the button by using the text contained in it, in this case we look for a button containing exactly "Dismiss"
driver.find_element(By.XPATH, '//button[text()="Dismiss"]').click()

How to correctly scroll a page to download each one of available zip files using Selenium on Python?

I'm trying to download all of the zip files from this page that don't end with the word 'CHECKSUM', so far I have managed to write a code that it's supposed to do that but it's not working as expected, here it is:
import time
import numpy as np
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
opt = Options() #the variable that will store the selenium options
opt.add_experimental_option("debuggerAddress", "localhost:9222") #this allows bulk-dozer to take control of your Chrome Browser in DevTools mode.
s = Service(r'C:\Users\ResetStoreX\AppData\Local\Programs\Python\Python39\Scripts\chromedriver.exe') #Use the chrome driver located at the corresponding path
driver = webdriver.Chrome(service=s, options=opt) #execute the chromedriver.exe with the previous conditions
#Why using MarkPrices: https://support.btse.com/en/support/solutions/articles/43000557589-index-price-and-mark-price#:~:text=Index%20Price%20is%20an%20important,of%20cryptocurrencies%20on%20major%20exchanges.&text=Mark%20Price%20is%20the%20price,be%20fair%20and%20manipulation%20resistant.
time.sleep(2)
if driver.current_url == 'https://data.binance.vision/?prefix=data/futures/um/daily/markPriceKlines/ALICEUSDT/1h/' :
number = 2 #initialize an int variable to 2 because the desired web elements in this page starts from 2
counter = 0
while number <= np.size(driver.find_elements(By.XPATH, '//*[#id="listing"]/tr')): #iterate over the tbody array
data_file_name = driver.find_element(By.XPATH, f'//*[#id="listing"]/tr[{number}]/td[1]/a').text
if data_file_name.endswith('CHECKSUM') == False:
current_data_file = driver.find_element(By.XPATH, f'//*[#id="listing"]/tr[{number}]/td[1]/a')
element_position = current_data_file.location
y_position = str(element_position.get('y'))
driver.execute_script(f"window.scrollBy(0,{y_position})", "") #scroll down the page to know what's being added
current_data_file.click()
print(f'saving {data_file_name}')
time.sleep(0.5)
counter += 1
number += 1
print(counter)
My problem occurs at the 20th element (ALICEUSDT-1h-2022-02-04.zip.CHECKSUM), the program stops and throws errors like the one down below:
ElementClickInterceptedException: element click intercepted: Element
is not clickable at point (418, 1294)
Or this other one with a negative position:
ElementClickInterceptedException: element click intercepted: Element
is not clickable at point (418, -1221)
So, I would like to know how could I improve the code above to handle the errors shown, I know it has everything to do with the scrollbar, but I ran out of ideas after using this other line y_position = str(element_position.get('y')+100) and keep getting the same errors.

Find the Twitter text box element with python selenium

I made my own Twitter complaint bot that tweets at my ISP if the network drops.
Code works perfect, until it has to find the Twitter textbox to type the tweet.
Main error is:
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
I have tried:
Adding time delays
Using Firefox Driver instead of Google
Adding page refreshes before the tweet_at_provider() looks for the textbox
Clicking the "Tweet" button to bring up the textbox to then try type in it
Using find.element_by_id but twitter changes id every pageload
When I comment out the first function call to test, it will find and type 6/10 times.
But when both functions are called the tweet_at_provider() always fails at grabbing the textbox and I get the StaleElement error.
import selenium, time, pyautogui
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException, NoSuchElementException, StaleElementReferenceException
PROMISED_DOWN = 200
PROMISED_UP = 10
CHROME_DRIVER_PATH = "C:\Development\chromedriver.exe"
GECKODRIVER_PATH = "C:\\Users\\meeha\\Desktop\\geckodriver\\geckodriver.exe"
TWITTER_USERNAME = "my_username"
TWITTER_PASSWORD = "my_password"
class InternetSpeedTwitterBot():
def __init__(self, driver_path):
self.driver = webdriver.Chrome(executable_path=driver_path)
self.down = 0
self.up = 0
def get_internet_speed(self):
self.driver.get("https://www.speedtest.net/")
self.driver.maximize_window()
time.sleep(2)
go = self.driver.find_element_by_xpath("//*[#id='container']/div/div[3]/div/div/div/div[2]/div[3]/div[1]/a/span[4]")
go.click()
time.sleep(40)
self.down = self.driver.find_element_by_xpath("//*[#id='container']/div/div[3]/div/div/div/div[2]/div[3]/div[3]/div/div[3]/div/div/div[2]/div[1]/div[2]/div/div[2]/span")
self.up = self.driver.find_element_by_xpath("//*[#id='container']/div/div[3]/div/div/div/div[2]/div[3]/div[3]/div/div[3]/div/div/div[2]/div[1]/div[3]/div/div[2]/span")
print(f"Download Speed: {self.down.text} Mbps")
print(f"Upload Speed: {self.up.text} Mbps")
time.sleep(3)
def tweet_at_provider(self):
self.driver.get("https://twitter.com/login")
self.driver.maximize_window()
time.sleep(3)
username = self.driver.find_element_by_name("session[username_or_email]")
password = self.driver.find_element_by_name("session[password]")
username.send_keys(TWITTER_USERNAME)
password.send_keys(TWITTER_PASSWORD)
password.submit()
time.sleep(5)
tweet_compose = self.driver.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/header/div/div/div/div[1]/div[3]/a/div/span/div/div/span/span')
tweet_compose.click()
time.sleep(2)
textbox = self.driver.find_element_by_xpath('//*[#id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div[3]/div/div/div/div[1]/div/div/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div/div/div[1]/div/div/div/div[2]/div/div/div/div')
textbox.send_keys(f"Hey #Ask_Spectrum, why is my internet speed {self.down.text} down / {self.up.text} up when I pay for {PROMISED_DOWN} down / {PROMISED_UP} up???")
bot = InternetSpeedTwitterBot(CHROME_DRIVER_PATH)
bot.get_internet_speed()
bot.tweet_at_provider()
I had the same error there and figured out that HTML tag was instantly changing as soon as I was typing something on the twitter text-box.
tackle this problem using XPATH of span tag that was showing up after typing space from my side. break tag is the initial tag when there is not any text prompted by you, only after you type anything turns into and that's when you have to copy XPATH and use it for your application

(Python)Guys plz help!I use selenium for making a simple bot(for yt)...and at the second button it says me list index out of range..the first runs ok

from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("link from chrome")
submit_button = driver.find_elements_by_xpath('//*[#id="dismiss-button"]/yt-button-renderer/a')[0]
submit_button.click()
time.sleep(5)
submit_button2 = driver.find_elements_by_xpath('//*[#id="introAgreeButton"]/span')[0]
#so here at the second button it says me that the list index is out of range..the program runs #with no problem but the second button doesnt work and it says me list index out of range
#the first button runs well..
submit_button2.click()
#please guys any help is acceptable!!!!
print (driver.title)
print (driver.current_url)

Fill a table or list of values in textarea in website using Splinter

I am trying to write a script to automate the search for multiple entries. Here is the input:
<mirna-1>
<mirna-2>
<mirna-3>
:
:
The website address is https://www.mirnet.ca/miRNet/faces/upload/MirUploadView.xhtml
How can I fill mirna-1,.... into miRNA list box. I have made the following attempts but failed:
gecko = os.path.normpath('~/geckodriver')
nirnet_Results = []`
browser = Browser(executable_path=gecko)
browser.visit('https://www.mirnet.ca/miRNet/faces/home.xhtml')
# click miRNA_list
xpath = '//*[#id="j_idt39:j_idt55"]'
browser.find_by_xpath(xpath).click()
time.sleep(1)
#Select Human
xpath = '//*[#id="form:j_idt34"]/div[3]/span'
browser.find_by_xpath(xpath).click()
time.sleep(1)
xpath = '//*[#id="form:j_idt34_1"]'
browser.find_by_xpath(xpath).click()
time.sleep(1)
# select miRBase ID type
xpath = '//*[#id="form:j_idt38"]/div[3]/span'
browser.find_by_xpath(xpath).click()
time.sleep(1)
xpath = '//*[#id="form:j_idt38_1"]'
browser.find_by_xpath(xpath).click()
time.sleep(1)
# select Tissue type
xpath = '//*[#id="form:sourceOpt"]/div[3]/span'
browser.find_by_xpath(xpath).click()
time.sleep(1)
xpath = '//*[#id="form:sourceOpt_1"]'
browser.find_by_xpath(xpath).click()
time.sleep(1)
# select target type
xpath = '//*[#id="form:targetOpts"]/div[3]/span'
browser.find_by_xpath(xpath).click()
time.sleep(1)
xpath = '//*[#id="form:targetOpts_1"]'
browser.find_by_xpath(xpath).click()
time.sleep(1)
# Fill miRNA list
xpath = '//*[#id="form:listData"]'
browser.find_by_xpath(xpath).click()
time.sleep(1)
for mir in known_mir:
browser.fill('form:listData',mir)
browser.fill('form:listData','\n')
I am getting success in selecting options in starting drop-down list and when I try to paste a list of names in the miRNA list, I am getting all the names like this:
<mirna-1><mirna-2><mirna-3>....
while the right format is each name should in the next line. How to do this. Kindly suggest. Thanks.
You can fill this form Using Selenium Chrome Driver and python.If you haven't use selenium before and would like use selenium.However here is the official website of selenium.
To install selenium use command line
pip install -U selenium
You need download chrome driver from following website website Chromedriver based on compatibility,such as which chrome browser supports by the chrome driver.Always prefer the latest versions of chrome driver and chrome browser.
Here is the selenium code which fills the form
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.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import time
driver=webdriver.Chrome('path to chrome driver exe') #example : 'C:\chromedriver\chromedriver.exe'
driver.get("https://www.mirnet.ca/miRNet/faces/upload/MirUploadView.xhtml")
#select Organism here
WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.ID,"form:j_idt34_label"))).click()
time.sleep(2)
element=WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH,"//div[#class='ui-selectonemenu-items-wrapper']//ul[#id='form:j_idt34_items']//li[contains(.,'H. sapiens (human)')]")))
ActionChains(driver).move_to_element(element).send_keys(Keys.UP).send_keys(Keys.DOWN).send_keys(Keys.ENTER).perform()
#select ID type here
WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.ID,"form:j_idt38_label"))).click()
time.sleep(2)
element=WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH,"//div[#class='ui-selectonemenu-items-wrapper']//ul[#id='form:j_idt38_items']//li[contains(.,'miRBase ID')]")))
ActionChains(driver).move_to_element(element).send_keys(Keys.UP).send_keys(Keys.DOWN).send_keys(Keys.ENTER).perform()
#select Tissue here
WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.ID,"form:sourceOpt_label"))).click()
time.sleep(2)
element=WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH,"//div[#class='ui-selectonemenu-items-wrapper']//ul[#id='form:sourceOpt_items']//li[contains(.,'Exosomes [1250]')]")))
ActionChains(driver).move_to_element(element).send_keys(Keys.UP).send_keys(Keys.DOWN).send_keys(Keys.ENTER).perform()
#select Target type here
WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.ID,"form:targetOpts_label"))).click()
time.sleep(2)
element=WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH,"//div[#class='ui-selectonemenu-items-wrapper']//ul[#id='form:targetOpts_items']//li[contains(.,'Genes')]")))
ActionChains(driver).move_to_element(element).send_keys(Keys.UP).send_keys(Keys.DOWN).send_keys(Keys.ENTER).perform()
items=['mirna-1','mirna-2','mirna-3','mirna-4']
for item in items:
element=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//textarea[#id='form:listData']")))
element.send_keys(item)
element.send_keys(Keys.RETURN)
Output:
Although I would recommend selenium for something like this, it seems you want to stick with splinter. The following fix using splinter. When reproducing this issue, each time browser.fill() is run it replaces the text already written. Since you didn't include what known_mir was in your code, I am assuming it is a list (correct me if I'm wrong). I tried combining what you wanted to put into the form and it worked.
Replace:
for mir in known_mir:
browser.fill('form:listData',mir)
browser.fill('form:listData','\n')
With:
browser.fill('form:listData', '\n'.join(mir for mir in known_mir))
All my code does is combine each of the strings in known_mir.

Resources