using chromedriver to pull table data from PowerBI based 'leaderboard' - python

How can I get the elements in the table?
Trying to locate the elements in a PowerBI table view embedded in a site here:
https://aptera.us/leaderboard/
manually, you have to select each row and use the right-click to 'copy selection'...I want to pull all values from all columns.
I have python with chromedriver running but times out before I find the table or elements.
I'm using this:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'.\chromedriver.exe')
driver.get("https://aptera.us/leaderboard/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='navigation-wrapper navigation-wrapper-big']//i[#title='row']"))).click()
print("Country:")
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[#class='bodyCells']//div[#class='pivotTableCellWrap cell-interactive ']")))[:20]])
driver.quit()
this looks to be the element I'm after:
/* #${ID}${name}","extra_options":[]};
/* ]]> */

Related

How to click on the Sign up button within Instagram Sign up page using Selenium and Python

I have been trying to create account within Instagram Sign up page with selenium and python, and I am able to enter all info into the text boxes, but for some reason, I am unable to click the "Sign up" button. I have tried using find element by XPath,CSS,ID, and class name but python still says that it cannot find the element. This is after entering all other necessary info on the form. Does anyone have any ideas?
The desired elements is a React element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.instagram.com/accounts/emailsignup/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='emailOrPhone']"))).send_keys("9876543210")
driver.find_element_by_xpath("//input[#name='fullName']").send_keys("aforkman")
driver.find_element_by_xpath("//input[#name='username']").send_keys("aforkman")
driver.find_element_by_xpath("//input[#name='password']").send_keys("aforkman")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Sign up']"))).click()
Browser Snapshot:
You can use this way :
driver.find_element_by_xpath("//button[contain(text() , "Sign up")]").click()

selenium clicking a href button

I am really new to selenium.
Currently, I am trying to use both selenium and beautifulsoup to do some webcrawling. The website that I am webcrawling on is https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp.
this is the code that I have for now.
driver = webdriver.Chrome(executable_path=path_to_chromebrowser)
driver.get("https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp")
input_area = driver.find_element_by_name("searchForm.genename")
input_area.send_keys("P2RY12")
searcher = driver.find_element_by_class_name("button")
searcher.click()
# table = driver.find_element_by_class_name("table7 table7-border")
# table.find_element_by_tag_name("a").click()
I am trying to click the first SNP ID that comes up, upon search. What would be the good way for me to click the href of the search result?
ON the webpage https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp to search for the Gene Name as P2RY12 and click the first SNP ID that comes up upon search you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#idgname[name='searchForm.genename']"))).send_keys("P2RY12")
driver.find_element_by_css_selector("button.button[type='submit']").click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "form[action^='/dogsdv2/com/exportFile'] table>tbody>tr td:nth-child(3)>a"))).click()
Browser Snapshot:
Try this:
firstsnpID = driver.find_element_by_xpath("(.//table[#class='table7 table7-border']/tbody/tr/td[3]/a)[1]")
firstsnpID.click()
you can not use compound classes to locate element using find_element_by_class_name
driver.find_element_by_xpath('/html/body/div/div[2]/div[2]/form/table/tbody/tr[1]/td[3]/a[1]').click()
If you need other ids:
for id in range(1,10):
driver.find_element_by_xpath('/html/body/div/div[2]/div[2]/form/table/tbody/tr[{}]/td[3]/a[1]'.format(id)).click()
sleep(5)
driver.back()
To click on first link on the table induce WebDriverWait() and element_to_be_clickable() and following CSS selector.
Code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path=path_to_chromebrowser)
driver.get("https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp")
input_area = driver.find_element_by_name("searchForm.genename")
input_area.send_keys("P2RY12")
searcher = driver.find_element_by_class_name("button")
searcher.click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"table.table7.table7-border td>a[href^='/dogsdv2/refsnp/showRefSNPDetail']"))).click()
To get all the link induce WebDriverWait() and visibility_of_all_elements_located() and get the href value then iterate each url
allelemets=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"table.table7.table7-border td>a[href^='/dogsdv2/refsnp/showRefSNPDetail']")))
allurls=[item.get_attribute('href') for item in allelemets]
print(allurls)
for link in allurls:
driver.get(link)

Finding page element using selenium(Python)

Im trying to find the username path for the login page on https://www.textnow.com/login. I've tried finding it by x_path, ID, Name, class but my bot just cant find it. Does anyone have any possible solutions that I would be able to try out ?
Source Code:
"SUDO FUNCTION: OPEN A NEW TAB FOR TEXT NOW AND LOG IN"
driver.implicitly_wait(3)
driver.execute_script("window.open('http://www.textnow.com/login','new window')")
textNowEmail = driver.find_element_by_id('txt-username')# still have not found username textfield
textNowEmail.send_keys(textNowUser)
#Set password code
textNowPass = driver.find_element_by_id('txt-password')
textNowPass.send_keys('fill')
This is the message im getting:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="txt-username"]"}
(Session info: chrome=78.0.3904.108)
To send a character sequence to the Email or Username and Password fields with in the website https://www.textnow.com/login you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://www.textnow.com/login")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.uikit-text-field__input#txt-username"))).send_keys("Xavier-Uriel-Espinal")
driver.find_element_by_css_selector("input.uikit-text-field__input#txt-password").send_keys("Xavier-Uriel-Espinal")
Using XPATH:
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://www.textnow.com/login")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='uikit-text-field__input' and #id='txt-username']"))).send_keys("Xavier-Uriel-Espinal")
driver.find_element_by_xpath("//input[#class='uikit-text-field__input' and #id='txt-password']").send_keys("Xavier-Uriel-Espinal")
Note : You have to add the following imports :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
You are opening a new window. Are you switching to it ? To make sure you are on the right window you can get page source by using "driver.get_source()" method and then evaluate the DOM.
Considering there are 2 window handles, you can switch to newly opened window using following :
required_window = driver.window_handles[1]
driver.switch_to_window(required_window)
Also try using "WebDriverWait" and "expected_conditions" to wait till required element is present by importing following:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
and then finding element using expected conditions:
WebDriverWait(driver,5).until(
EC.presence_of_element_located((By.ID, "txt-username")))
When you are opening new window with execute_script your window handle is still in the original window. You need to switch window.
You can check all the windows available with driver.window_hanldes
For your case just use
driver.switch_to.window(driver.window_handles[1])
after opening new window
then proceed with rest of your code

How can I submit this form with selenium in python?

I've been trying to make a Python script to login into a certain website, navigate through the menu, fill out a form and save the file it generates to a folder.
I am having some problems to submit the form.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
options.add_argument("--disable-extensions")
browser = webdriver.Chrome()
browser.get("https://directa.natal.rn.gov.br/")
WebDriverWait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame[name='mainsystem'][src^='main']")))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input[name='usuario']"))).send_keys("11844691000126")
browser.find_element_by_css_selector("input.input[name='senha']").send_keys("Link2007")
browser.find_element_by_css_selector("button.btn[name='acessar']").click()
Once you fill up the required fields then to submit the form you can use the following solution:
Code Block:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
# options.add_argument('disable-infobars')
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://directa.natal.rn.gov.br/")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame[name='mainsystem'][src^='main']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input[name='usuario']"))).send_keys("11844691000126")
driver.find_element_by_css_selector("input.input[name='senha']").send_keys("Link2007")
driver.find_element_by_css_selector("button.btn#acessar").click()
Browser Snapshot:
You have to switch to the iframe, because your element is not in the main website, is inside iframe.
iframes = browser.find_elements_by_xpath("//iframe[#name='mainsystem']")
print("Iframes: %s" %len(iframes))
browser.switch_to_frame(iframes[0])

How to select the option Books from the dropdown listbox within https://www.amazon.in/ using Selenium in Python?

How to select the option books from the dropdown listbox within https://www.amazon.in/ using Selenium in Python?
I am trying the code:
driver.find_element_by_xpath("//*[#id='searchDropdownBox']").send_keys('Books')
Try this:
select = Select(driver.find_element_by_id('searchDropdownBox'))
# select by visible text
select.select_by_visible_text('Books')
The drop down list for the categories is within a <select> tag so ideally you need to use the select class inducing WebDriverWait for the desired element to be clickable and you can use the following solution:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.amazon.in/')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='nav-search-label']")))
mySelect = Select(driver.find_element_by_xpath("//select[#id='searchDropdownBox']"))
mySelect.select_by_visible_text('Books')
print((mySelect.first_selected_option).text)
Console Output:
Books
Browser Snapshot:

Categories

Resources