Python Selenium pop-up - python

So, I've decided to make TikTok massfollower, but can't go further than login.
from selenium.webdriver import Firefox
from time import sleep
browser = Firefox()
browser.get('https://www.tiktok.com/foryou?lang=ru')
login = browser.find_element_by_class_name('jsx-3665539393')
login.click()
sleep(10)
login2 = browser.find_element_by_class_name('channel-name-2Dwny')
login2.click()
It goes to TikTok website, then it presses the log-in button, and then there is pop-up window(I hope that's the right way to call it). But program can't find elements on this pop-up window, I thought that I should wait for pop-up to load, but there is no difference.

The element you are after is inside an iframe you need to switch to iframe first in order to access the element.
Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it() and following css selector.
Induce WebDriverWait() and wait for element_to_be_clickable()
browser.get('https://www.tiktok.com/foryou?lang=ru')
#Click on Login
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"a.jsx-3665539393"))).click()
#Click cookeis button
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.button-wrapper>button"))).click()
WebDriverWait(browser,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src*='tiktok']")))
browser.execute_script("arguments[0].click();",WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.close-modal-8dfIo"))))
You need to import following libraries
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

Related

Selenium Radio Button not Functioning

I wish to create a bot for automatically fill in Google Form survey, which is filled with radio buttons. When I am trying to fill in the form, the first Gender radio button was successfully clicked. However, the second age group button was not? Is this a bug?
import time
from selenium import webdriver
browser = webdriver.Chrome(executable_path='C:/Users/ryan/Downloads/chromedriver.exe')
browser.get('https://docs.google.com/forms/d/e/1FAIpQLScA8I-O5FQoYPwU6yoYJmAwrIJdBB/viewform?fbzx=-1760864547538697754')
RadioGender= browser.find_element_by_xpath('//*[#id="i5"]/div[3]/div')
RadioGender.click()
RadioAge= browser.find_element_by_xpath('//*[#id="i18"]/div[3]/div/div')
RadioAge.click()
To handle dynamic element use WebDriverWait() and wait for element to be clickable and following locator strategy.
browser.get("https://docs.google.com/forms/d/e/1FAIpQLScA8I-O5FQoYPwU6yoYJmAwrIJdBBt1MqEgoQOz9oHWWmTY1Q/viewform?fbzx=-1760864547538697754")
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//span[text()="Next"]'))).click()
RadioGender= WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#data-value="Male"]')))
RadioGender.click()
RadioAge= WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#data-value="21-23"]')))
RadioAge.click()
You need to import following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Note: If you would like to select other age range, you need pass those values as data value.

How can I crawl Dynamic website with Selenium? There's error 'NoSuchElementException'

I've newly started to study Python and encountered NoSuchElementException while I'm doing crawling with SELENIUM.
I think target site is 'Dynamic webpage' hence I searched various way and tried but..... failed.
Would you help me? Thank in advance
Target site : http://www.kofiabond.or.kr/index_en.html
What I need : Click area on below picture and move to that page
enter image description here
Below is the code which I made:
from selenium import webdriver
driver=webdriver.Chrome('C:/Users/SMH/chromedriver.exe')
url='http://www.kofiabond.or.kr/index_en.html'
driver.get(url)
driver.maximize_window()
xpath = "//*[#id='genLv1_0_imgLv1']"
open_btn=driver.find_element_by_xpath(xpath)
driver.execute_script(open_btn)
To click on Final Quotation Yields link which is present inside an iframe you need to switch iframe first then hover on the main menu to element become visible on the page and then click on the element.
Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it()
Induce WebDriverWait() and wait for visibility_of_element_located() and hover on the element
Induce WebDriverWait() and wait for element_to_be_clickable()
Code:
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
from selenium.webdriver import ActionChains
driver=webdriver.Chrome("C:/Users/SMH/chromedriver.exe")
driver.get('http://www.kofiabond.or.kr/index_en.html')
#Switch to iframe
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.NAME, 'fraAMAKMain')))
element=WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//a[./img[#alt='bond Interest Rates']]")))
#Hover main menu
ActionChains(driver).move_to_element(element).perform()
#Click on the element once become clickable
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[./div[text()='Final Quotation Yields']]"))).click()
Browser snapshot

clicking on button in iframe with python

I am trying to access website with python selenium. I am going to place that I cant click on the button. any help please?
please find my code on the below:
from selenium import webdriver
from time import sleep
path_to_chromedriver = "chromedriver.exe"
driver = webdriver.Chrome(path_to_chromedriver)
url = 'https://www.mail.com/'
driver.get(url)
how to click on button "Agree and Continue" using css selector ?
The element Agree and Continue is present inside nested iframe you need to switch both the iframe first and then click on the element.
Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it()
Induce WebDriverWait() and wait for element_to_be_clickable()
driver.get("https://www.mail.com/")
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.permission-core-iframe")))
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://plus.mail']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[id='onetrust-accept-btn-handler']"))).click()
you need to import below libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Jump out from iframe you need to use.
driver.switch_to.default_content()

python selenium, cant't click element

i'm having a problem with selenium. I load this page: Investing 3M, and i want to click got it in a pop-out window.
I already tryed with this code bellow, but it doesn't work:
driver = webdriver.Chrome()
driver.get("https://www.investing.com/equities/3m-co-financial-summary")
driver.implicitly_wait(10)
x =driver.find_elements_by_xpath("/html/body/div[8]/div[1]/div/div[2]/div[2]/a[1]")
print(len(x))
print(x)
Can anyone provide any solution to this?
The element is present inside an iframe.You need to switch to frame first.
To handle dynamic element you need to
Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it() and following xpath
Induce WebDriverWait() and wait for element_to_be_clickable() and following xpath
code:
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 import webdriver
driver = webdriver.Chrome()
driver.get("https://www.investing.com/equities/3m-co-financial-summary")
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='TrustArc Cookie Consent Manager']")))
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Got it']"))).click()
Browser snapshot after clicking

Selenium - Switch to dynamic iframe after onclick button

I am trying to switch into a new frame, which opens after clicking a button. Unfortunately, I receive a None value.
driver = webdriver.Firefox()
driver.get('https://www.milanuncios.com/dacia-de-segunda-mano/dacia-sandero-1-5-dci-exportacion-323650137.htm')
time.sleep(5)
driver.find_element_by_xpath('//button[#id="pagAnuShowContactForm"]').click()
time.sleep(5)
Till here, it works to open the contact information. Here, I would like to perform actions in the new, opened window.
I tried the following options:
1.
contact = driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
2.
contact = driver.find_element_by_xpath('//div[#class="telefonos"]')
To get the telephone number.The element is present inside iframe ID ifrw you need to switch to iframe first.
Induce WebDriverWait And frame_to_be_available_and_switch_to_it()
Induce WebDriverWait And visibility_of_element_located()
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
driver = webdriver.Firefox()
driver.get('https://www.milanuncios.com/dacia-de-segunda-mano/dacia-sandero-1-5-dci-exportacion-323650137.htm')
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[#id='pagAnuShowContactForm']"))).click()
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"ifrw")))
print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,".telefonos"))).text.strip())
driver.close()
Output:
663473583

Categories

Resources