How to select a frame using selenium with target? - python

link = driver.find_element_by_partial_link_text('/wse/gupmenug.menu?p_sistema_c=ESCOLAR&p_sistemaid_n=1&p_menupredid_n=1&p_pidm_n=425370')
How can i fix this?

Your locator indeed looks invalid.
using the expected conditions and better locators it will be something like this:
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
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//a[text()='ALUMNOS']"))).click()
wait.until(EC.visibility_of_element_located((By.XPATH, "//a[text()='ESCOLAR']"))).click()

Related

Locate the element

# import webdriver
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
from selenium.common.exceptions import WebDriverException
# create webdriver object
driver = webdriver.Firefox()
url = "https://www.zalando.no/tommy-jeans-tjm-essential-down-vest-vest-court-blue-tob22t06j-k11.html"
driver.get(url)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[#id="uc-btn-accept-banner"]'))).click()
driver.find_element(By.XPATH, "/html/body/div[4]/div/div[1]/div/div/div[2]/div[1]/x-wrapper-re-1-6/div/div[2]/button/span").click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"/html/body/div[9]/div/div[3]/div/form/div/div[4]/div/label/span/div/span"))).click()
I'm trying to create a sneaker bot for fun. When I click to select shoes size I can't locate the element for some reason.
wait.until(EC.element_to_be_clickable((By.XPATH,"(//span[.='XS'])[1]"))).click()
Should work for the XS size if you want since most of the elements seem dynamic.
You are using absolute xpath, please switch to relative path.
Also, it's a good practice to create object of WebDriverWait once and then use the reference in other places in your code.
driver = webdriver.Firefox()
driver.maximize_window()
wait = WebDriverWait(driver, 30)
url = "https://www.zalando.no/tommy-jeans-tjm-essential-down-vest-vest-court-blue-tob22t06j-k11.html"
driver.get(url)
try:
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[#id='uc-btn-accept-banner']"))).click()
except:
pass
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Legg i handlekurven']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='L']"))).click()
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How to check if an element is not displayed in selenium python

How could I check to see if an element is not displayed. I would think it looks something like this.
if(element.is_not_displayed):
doSomething()
else
doSomethingElse()
To start with, there is no is_not_displayed attribute in Selenium.
To simulate a similar logic, instead of an if-else loop you may use a try-except{} loop inducing WebDriverWait for the invisibility_of_element() and you can use the following Locator Strategy:
try:
WebDriverWait(driver, 30).until(EC.invisibility_of_element(element))
doSomething()
except TimeoutException:
doSomethingElse()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
You have to use until_not as shown bellow
WebDriverWait(driver, "time you want to wait".until_not(EC.presence_of_element_located((By.ID,"someID")))
Example:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID,"transpdiv-0"))) #here yout wait to the element appear
WebDriverWait(driver, 300).until_not(EC.presence_of_element_located((By.ID,"transpdiv-0"))) #here you wait the element disappear
Note: you have to add the same imports like undetect Selenium says:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

how to excract text from web site using selenium

i have Code like this:
<mat-label _ngcontent-vrc-c49="" class="filingCount ng-star-inserted">2 RRR</mat-label>
and i want value : 2 RRR
i tried with:
element = driver.find_element_by_xpath('//*[#id="maincontentid"]/app-dashboard/app-itr-status/div[4]/mat-label')
print(element.text)
but it's give the zero
Possibly you have to add wait until the element is visible. Like this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
element = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="maincontentid"]/app-dashboard/app-itr-status/div[4]/mat-label')))
print(element.text)
If still not working validate your locator correctness.

Unable to select drop down using Python Selenium

I am trying to scrape this website: http://sekolah.data.kemdikbud.go.id/
I want to select "Jenjang" field, "SMA" value. After that, need to click "Cari Sekolah" button
Unfortunately, my code does not work. I manage to select SMA but then can't click "Cari Sekolah" to start the query. Anyone knows how to fix this.
Here is my code:
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
import time
from selenium.webdriver.support.ui import Select
option = webdriver.ChromeOptions()
option.add_argument('--incognito')
webdriver = "/Users/rs26/Desktop/learnpython/web/chromedriver"
driver = Chrome(executable_path=webdriver, chrome_options=option)
url="http://sekolah.data.kemdikbud.go.id/"
driver.get(url)
wait = WebDriverWait(driver,15)
select_element = Select(driver.find_element_by_id("bentuk"))
select_element.select_by_value("SMA")
wait.until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Cari Sekolah']"))).click()
Please find below solution to select from custom dropdown
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
# # Solution 1:
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.get('http://sekolah.data.kemdikbud.go.id/')
driver.maximize_window()
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "select2-bentuk-container")))
element.click()
list=WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='select2-search__field']")))
list.send_keys("SMA")
select=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "select2-results__option")))
select.click()
You can use form button[type=submit] css selector to click.
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"form button[type=submit]"))).click()

Selenium Python Script throws no element error exception even though the x path is right?

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
from selenium.webdriver.common.keys import Keys
driver=webdriver.Chrome()
driver.get("https://paytm.com/")
driver.maximize_window()
driver.find_element_by_class_name("login").click()
driver.implicitly_wait(10)
driver.find_element_by_xpath("//md-input-container[#class='md-default-theme md-input-invalid']/input[#id='input_0']").send_keys("99991221212")
In the above code, I have verified the xpath using fire bug its highlighting the correct element. But when the script run its failing? Can you help me folks?
In selenium each frame is treated individually. Since the login is in a separate iframe element, you need to switch to it first using:
iframe = driver.find_elements_by_tag_name('iframe')[0]
driver.switch_to_frame(iframe)
Before trying to interact with it's elements.
Or in this case, you would wait for the frame to exist, and it would be:
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.Chrome()
driver.get("https://paytm.com/")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "login"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME, "iframe")))
_input = wait.until(EC.visibility_of_element_located((By.ID,"input_0")))
_input.send_keys("99991221212")
You should try using WebDriverWait to wait until input element visible on the page as below :-
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.Chrome()
driver.get("https://paytm.com/")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "login"))).click()
#now switch to iframe first
wait.until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME, "iframe")))
input = wait.until(EC.visibility_of_element_located((By.ID, "input_0")))
input.send_keys("99991221212")
Hope it helps...:)

Categories

Resources