click function not rendering the page - python

I am trying to retrieve PNR details but the click function runs into a timeout exception. What could be the possible issue that causes this timeout?
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.common.keys import Keys
driver = webdriver.Chrome(executable_path=r'D:/Chrome driver/chromedriver.exe')
driver.get("link")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='PNRId']"))).send_keys("QPDYUX")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='GstRetrievePageInteraction']"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "gst-invoice-list.list-inline")))
elements = driver.find_elements(By.CLASS_NAME, "gst-invoice-list.list-inline")
It gives following error TimeoutException: timeout: Timed out receiving message from renderer: 300.000 (Session info: chrome=87.0.4280.141)
How can I move forward with this?

Try adding the following to stop the website from knowing it's a bot.
from selenium.webdriver.chrome.options import Options
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(executable_path=r'D:/Chrome driver/chromedriver.exe',options=options)
Now for the following:
wait = WebDriverWait(driver, 10)
driver.get("link")
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#id='PNRId']"))).send_keys("QPDYUX")
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#id='GstRetrievePageInteraction']"))).click()
elements = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".gst-invoice-list.list-inline")))
for elem in elements:
print(elem.text)
Outputs:
GST Invoice No. View/Print
MH1202106AB57221 View Invoice Print Invoice
MH2202106AB78553 View Invoice Print Invoice

Related

Unable to locate element within an iframe while the element actually exists

No matter how long I wait, it seems selenium can't find the "watch_online" button.
I've tried both by XPath, full XPath, and CSS selector.
I want to get the href link from the "Watch Online" button.
import os
import glob
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
# Create local browser cache
browser_data_location = "browser_data"
options = webdriver.ChromeOptions()
options.add_argument(f'user-data-dir={os.getcwd()}/{browser_data_location}')
i_m_not_a_robot_xpath = '//*[#id="landing"]/div[2]/center/img'
generate_link_xpath = '//*[#id="generater"]/img'
click_to_continue = '//*[#id="showlink"]'
get_download_link = '/html/body/section/div/div/div/center/a'
watch_online = '//*[#id="download-hidden"]/a'
with webdriver.Chrome(options=options, ) as driver:
wait = WebDriverWait(driver, 10)
time.sleep(2)
driver.get(
"https://www.rtilinks.com/?82255aba71=RmwzVDZObDFBdDQvay8zRjhiaStoM004Ymd1T201MnBQelJpdW5oK1UxeGFvbFZUY1FEVXMrY0o2UnhqeGxOOFlwN3JlUElad2h0ek9pQ1ZFZndXSG9UTzA1aFpmTEhoanBVUldEYWwwWVU9")
# wait.until(ec.element_to_be_clickable((By.CSS_SELECTOR, upload_box_css))).send_keys(file)
wait.until(ec.element_to_be_clickable((By.XPATH, i_m_not_a_robot_xpath))).click()
# time.sleep(1)
wait.until(ec.element_to_be_clickable((By.XPATH, generate_link_xpath))).click()
wait.until(ec.element_to_be_clickable((By.XPATH, click_to_continue))).click()
# original_window = driver.current_window_handle
driver.close()
driver.switch_to.window(driver.window_handles[0])
wait.until(ec.element_to_be_clickable((By.XPATH, get_download_link))).click()
time.sleep(2)
link = driver.find_element(By.XPATH, watch_online)
print(link.get_attribute('href'))
The element Watch Online is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#src, 'https://purefiles.in')]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Watch Online"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://purefiles.in']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.button.is-success"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='button is-success' and contains(., 'Watch Online')]"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#src, 'https://purefiles.in')]")))
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
Reference
You can find a couple of relevant discussions in:
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

Python Selenium cannot find input

I am trying to target the login input fields on this Website.
So far I have tried using, ID, Name. Add a wait until present or wait until clickable but no luck.
Using chropath to try and get an xmlpath does not work on this element either for some reason.
That is because, there is an iframe so you need to switch to iframe first.
try below code :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.genedx.com/signin/")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#id='catapultCookie']"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "div#biopeople-login-registration iframe")))
wait.until(EC.element_to_be_clickable((By.ID, "loginEmail"))).send_keys('user name here')
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 click a link on a web page using Python Selenium?

I am trying to click a link on a web page with Python Selenium but I am getting this exception:
no such element: Unable to locate element:
I have already tried using find_element_by_xpath, find_element_by_partial_link_text and find_element_by_link_text.
This is my code:
import time
from selenium import webdriver
driver = webdriver.Chrome('C:/Users/me/Downloads/projetos/chromedriver_win32/chromedriver.exe') # Optional argument, if not specified will search path.
driver.get('http://10.7.0.4/web/guest/br/websys/webArch/mainFrame.cgi');
time.sleep(10) # Let the user actually see something!
#elem = driver.find_element_by_xpath('//*[#id="machine"]/div[1]/div[1]/dl[2]/dt/a')
elem = driver.find_element_by_link_text('Mensagens (2item(ns))')
elem.click()
print("Fim...")
This is the element I need to click:
Mensagens (2item(ns))
You can try with explicit waits and with the customized css :
CSS_SELECTOR :
a[href*='../../websys/webArch/getStatus.cgi']
Sample code :
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='../../websys/webArch/getStatus.cgi']"))).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
Update 1 :
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#href, 'ebsys/webArch/getStatus.cgi') and contains(text(), 'Mensagens')]"))).click()

Can you help me use Selenium to click Add To Cart button?

I am trying to do a tutorial and learn Selenium in python however i cant seem to get Selenium to click the "Add To Cart" button using either find_element_by_class or find_element_by_XPATH. The problem is to check if the item is out of stock, and if it is out of stock then to refresh the webpage and restart the script. If the item is in stock then it should click "Add To Cart"
I am using:
Python v3.9
Chrome v87
This is the URL i am practicing on:
https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html
And this is my current code for the clicking:
# Selenium Tutorial #1
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
import time
driver = webdriver.Chrome(r"C:\Users\Ste1337\Desktop\chromedriver\chromedriver.exe")
driver.get("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html")
#search = driver.find_element_by_id(ContentPlaceHolder1_NotifyBtn)
link = driver.find_element_by_id("onetrust-accept-btn-handler")
link.click
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler"))
)
element.click()
except Exception:
pass
driver.implicitly_wait(2)
try:
element = WebDriverWait(driver, 1).until(
EC.presence_of_element_located((By.XPATH, "email-desktop"))
)
time.sleep(1)
browser.refresh()
except:
driver.find_element_by_class_name("Button__StyledButton-bvTPUF hZIOeU Button-jyKNMA GZkwS")
link.click()
You cannot pass multiple classes to find_element_by_class_name. Instead use driver.find_element_by_css_selector or xpath.
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
import time
driver = webdriver.Chrome(r"C:\Users\Frank\Documents\Python Scripts\chromedriver_win32\chromedriver.exe")
driver.get("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html")
#search = driver.find_element_by_id(ContentPlaceHolder1_NotifyBtn)
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler"))
)
element.click()
except Exception:
pass
driver.implicitly_wait(2)
try:
element = WebDriverWait(driver, 1).until(
EC.presence_of_element_located((By.XPATH, "email-desktop"))
)
time.sleep(5)
driver.refresh()
except:
button = driver.find_element_by_css_selector("button.Button__StyledButton-bvTPUF.hZIOeU.Button-jyKNMA.GZkwS")
Next, if you would call button.is_displayed() it will return False, because it is a hidden element. This means you cannot interact with it using button.click(). Instead, you can use javascript to click the button.
driver.execute_script("arguments[0].click();", button)

why is selenium reaching timeout when the element is there

i have a problem code below where the element becomes visible and is not clicked. i tried both css selectors and the xpath.
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
PROXY = "socks5://184.178.172.13:15311" # IP:PORT or HOST:PORT
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://nitrogensports.eu/sport/tennis/starting-soon')
wait = WebDriverWait(driver, 30)
table = wait.until(EC.presence_of_element_located((By.XPATH, '//*[#id="modal-welcome-new-button"]')))
table.click()
table = wait.until(EC.presence_of_element_located((By.XPATH, '//div[class="div.events-result-set"]')))
print("finished")
time.sleep(30)
driver.close()
As per your question the element identified as (By.XPATH, '//*[#id="modal-welcome-new-button"]') is not getting clicked.
Once the wait is over and the element is identified and returned back, moving forward as you are invoking click() method so instead of using the expected_conditions method presence_of_element_located you need to use the method element_to_be_clickable as follows :
driver.get('https://nitrogensports.eu/sport/tennis/starting-soon')
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='party-button highlightable-button highlighted' and #id='modal-welcome-new-button']"))).click()

Categories