How to use click in Selenium for opening links - python

I am having trouble with Selenium lately.
I'm trying to get all the URL from
http://cimex.co/resources.html
and open it on a new tab.
I'm trying to achieve with this code:
import selenium, os
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://cimex.co/resources.html')
links = browser.find_elements_by_css_selector('a[href^="https"]')
links[0].click()
While I am facing another problem is there any chance if it detects Firefox running just open the URL in a new tab rather than opening Firefox as a new app.
Thanks.
Sorry for my bad English.

I already had to do that, it was with the ChromeDriver, I cannot say if it will works the same with Firefox, but here is the code:
def Browser(object):
def __init__(self):
self.driver = webdriver.Chrome(driver_path)
self._tabs = {'default': self.driver.window_handles[0]}
def new_tab(self, name):
'''
Create new tab `name`.
name New tab name
'''
self.driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
self._tabs[name] = self.driver.window_handles[-1]
def switch_tab(self, name):
'''
Switch to given tab.
name Tab name to switch
'''
self.driver.switch_to_window(self._tabs[name])
browser = Browser()
browser.driver.get('http://cimex.co/resources.html')
links = browser.driver.find_elements_by_css_selector('a[href^="https"]')
url = links[0].get_attribute('href')
# open new tab
browser.new_tab(url, 'tab-name')
# switch to new tab
browser.switch_tab('tab-name')
# back to default tab
browser.switch_tab('default')

You can try this code :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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.action_chains import ActionChains
browser = webdriver.Firefox(executable_path = r'D:/Automation/geckodriver.exe')
browser.get("http://cimex.co/resources.html")
wait = WebDriverWait(browser, 30)
main_tab = browser.current_window_handle
print(main_tab)
links = browser.find_elements_by_tag_name('a')
size = len(links)
print ('this the length of list:',size)
i = 0
while i<size:
ActionChains(browser).key_down(Keys.CONTROL).click(links[i]).key_up(Keys.CONTROL).perform()
browser.switch_to_window(main_tab)
i=i+1;
if i >= size:
break
print(" here you quit the driver as per your wish")

from selenium import webdriver
browser = webdriver.Firefox(gecko driver location)
browser.get('http://cimex.co/resources.html')
links = browser.find_elements_by_css_selector('a[href^="https"]')
for link in links:
link.click()

Related

Webdriver ( Selenium ) Cannot find the element

`
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from time import sleep
import os
login_page = "https://fap.fpt.edu.vn/Default.aspx"
# page = "https://fap.fpt.edu.vn/Report/ScheduleOfWeek.aspx"
email = ""
password = ""
options = Options()
options.add_argument("--window-size=1920,1080")
options.binary_location = "C:\Program Files\Google\Chrome\Application\chrome.exe"
driver = webdriver.Chrome(options = options)
driver.get(login_page)
login1 = driver.find_element("xpath","//div[#class='abcRioButtonContentWrapper']").click()
driver.find_element(By.NAME, "identifier").send_keys(email)
sleep(3)
driver.find_element(By.ID, "identifierNext").click()
sleep(2)
driver.find_element(By.NAME, "password").send_keys(password)
sleep(2)
driver.find_element(By.ID, "passwordNext").click()
sleep(999999)
`
I believe i chose the right By.NAME ( "indentifier" ) but the code still not work and return message no such element.
I tried to change the syntax, using xpath change By.NAME to By.ID but it still not work
Google login page opens in a new window. So you need to switch to this window before interacting with it. So you need to use this code (the first and the last lines are from your code and between is the part you need to add):
login1 = driver.find_element("xpath","//div[#class='abcRioButtonContentWrapper']").click()
windows = driver.window_handles
driver.switch_to.window(windows[1])
driver.find_element(By.NAME, "identifier").send_keys(email)
And after you've finished with the login you will need to switch back to the main window. To do that you can use this code:
driver.switch_to.window(windows[0])
And then work with the content of the page
No, don't use name of jsname here, because they get filled with random data.
Just find your Google account name or your email address by text and click it:
userAccount = driver.find_element_by_xpath("//*[text()='YourGoogleAccountName']")
userAccount.click()

How to disable whatsapp open-url pop-up while scraping

I am currently working on a project where I have to scrape individual WhatsApp profile and get certain details from it using api.whatsapp. With this link immediately the chrome browser pops up it displays a notification, which immediately stops all the code from running, I need to be able to bypass the pop up.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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.chrome.options import Options
# location of web driver
PATH = 'C:\Program Files (x86)\chromedriver'
# selecting what web browser to use
driver = webdriver.Chrome(PATH)
driver.get(f'https://api.whatsapp.com/send/?phone=%2B234{n}&text&type=phone_number&app_absent=0')
n = 88888888
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.LINK_TEXT, "Continue to Chat"))
)
except:
driver.quit()
I tried using the WebDriverWait command thinking id be able to manually click cancel the pop-up tab and the code would continue.
to skip alert open whatsapp frist
driver.get('https://web.whatsapp.com/')
than use
js = """
var [ num ] = [ arguments[0]];
function openChat (t) {
var e;
t&&((e=document.createElement("a")).setAttribute("href","whatsapp://send?phone="+t),document.body.appendChild(e),e.click(),e.outerHTML="",setTimeout(1,1e3))
}
return openChat(num)
"""
driver.execute_script(js, n)

Can't scroll down in web page

In python and selenium I can't scroll down in this page https://melkemun.com/ to show & get another post
driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
This page uses body to crop displayed area and real scrolled area is
<div class="scroll-body" onscroll="loadMore(event)">
This works for me in JavaScript console (in DevTools in Firefox)
item = document.getElementsByClassName('scroll-body')[0];
item.scrollTo(0, item.scrollHeight);
so you may need
driver.execute_script("item=document.getElementsByClassName('scroll-body')[0];item.scrollTo(0,item.scrollHeight);")
Page uses Shadow Root and code works only with Chrome. (Selenium: 4.4.0)
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
#from webdriver_manager.firefox import GeckoDriverManager
import time
url = 'https://melkemun.com/'
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
#driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))
driver.get(url)
print('sleep')
time.sleep(5)
shadow = driver.find_element(By.ID, 'stting').shadow_root
#print(shadow)
# select city
shadow.find_elements(By.CLASS_NAME, "btn-city")[0].click()
print('sleep')
time.sleep(5)
for i in range(5):
print('scrolling:', i)
#driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
driver.execute_script('item=document.getElementsByClassName("scroll-body")[0];item.scrollTo(0,item.scrollHeight);')
print('sleep')
time.sleep(5)

Scraping flex-element Selenium Python

I am trying to scrape some tennis statistics starting from 01-01-2019.
For this I try to scrape the following webpage with selenium: https://www.sofascore.com/de/tennis/2019-01-01
When I click on the first match manually the container on the right side changes and shows the statistics.
This is what I want to access automatically.
When I try to click on the element with selenium it redirects me to another page.
Can anyone tell me why it is not just showing the same content as by manually clicking and how I can solve this issue?
Here is my code:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
import time
options = Options()
options.binary_location = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
browser = webdriver.Chrome(chrome_options = options)
url = 'https://www.sofascore.com/de/tennis/2019-01-01'
browser.get(url)
browser.maximize_window()
xpath = '/html/body/div[1]/main/div/div[2]/div/div[3]/div[2]/div/div/div/div/div[2]/a/div'
browser.find_element_by_xpath(xpath).click()
time.sleep(2)
browser.close()`
You can use the below xpath :
//div[contains(#class, 'Col-pm5mcz-')]//descendant::div[contains(#class, 'styles__StyledWidget-')]
and get the innerHTML of that using get_attribute method
Code :
url = "https://www.sofascore.com/de/tennis/2019-01-01"
driver.get(url)
xpath = '/html/body/div[1]/main/div/div[2]/div/div[3]/div[2]/div/div/div/div/div[2]/a/div'
driver.find_element_by_xpath(xpath).click()
sleep(2)
details = driver.find_element_by_xpath("//div[contains(#class, 'Col-pm5mcz-')]//descendant::div[contains(#class, 'styles__StyledWidget-')]").get_attribute('innerHTML')
print(details)
The xpath that you are using is absolute xpath /html/body/div[1]/main/div/div[2]/div/div[3]/div[2]/div/div/div/div/div[2]/a/div
try to replace that with Relative xpath.
See if this works
tableRows = driver.find_elements_by_xpath(".//div[#class='ReactVirtualized__Grid ReactVirtualized__List']//following::div/a[contains(#class,'EventCellstyles__Link')]")
for e in tableRows:
e.click()
//You can add implicit wait here for the statics section to load
driver.find_element_by_xpath(".//a[text()='Statistiken']").click()

How can I paste a string to textarea in browser with python?

I want to send a string to the web page whose text field name is "inputfield". Actually, I can send the word to the page, but when I run the program, a new "chrome" page opens, which is used for testing purposes. However, I want to send a string to the field on a chrome page that is already open.
Here my code:
from selenium import webdriver
import time
url = "https://10fastfingers.com/typing-test/turkish"
options = webdriver.ChromeOptions()
options.binary_location = r"C://Program Files//Google//Chrome//Application//chrome.exe"
chrome_driver_binary = 'chromedriver.exe'
options.add_argument('headless')
driver = webdriver.Chrome(chrome_driver_binary, options=options)
driver.get(url)
driver.implicitly_wait(10)
text_area = driver.find_element_by_id('inputfield')
text_area.send_keys("Hello")
Nothing happens when I run this code. Can you please help? Can you run it by putting a sample web page in the url part?
Thank you.
EDIT: It is working when I deleted options. But still opening a new page when I run it. Is there a way use a page which already open on background.
chrome_driver_binary = 'chromedriver.exe'
driver = webdriver.Chrome(chrome_driver_binary)
driver.get('https://10fastfingers.com/typing-test/turkish')
text_area = driver.find_element_by_id('inputfield')
text_area.send_keys("Hello")
Click the popup prior to sending keys.
driver.get('https://10fastfingers.com/typing-test/turkish')
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "CybotCookiebotDialogBodyLevelButtonLevelOptinAllowallSelectionWrapper"))).click()
text_area = wait.until(EC.element_to_be_clickable((By.ID, "inputfield")))
text_area.send_keys("Hello")
Imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I am not sure what is your question but if the issue is multiple tabs or windows being opened then:
you can switch between the windows as:
// you can move to specific handle
chwd = driver.window_handles
print(chwd)
driver.switch_to.window(chwd[-1])
you should shoul switch to the correct window before you can interact with elements on that window
just switch to the window that was already opened bypassing the index
If the problem is that you want to interact with an already opened chrome then you should follow below steps:
Start chrome with debug port:
<path>\chrome.exe" --remote-debugging-port=1559
Python :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:1559")
driver = webdriver.Chrome(options=chrome_options)

Categories