why is selenium reaching timeout when the element is there - python

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()

Related

not able to get the search button with selenium python

I am trying to scrap articles from this website. I manage to do the login part but when I try to click on the search button and send the values I got a timeout error. I try running the selenium with start-maximize option and I noticed the page don't seem to load.
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="search__input"]')))
input_text = elem.find_element(by=By.XPATH, value='//*[#id="search__input"]').click()
input_text.send_keys("Anthony Albanese")
print(input_text.get_attribute('value'))
I have tried to use the get_attribute('innerHtml') and I got the search button HTML but I have to send the keys so it does not seems to be of any use. This is the error that I got
screenshot. What should I do to send in the search terms?
There are 2 elements on that page matching //*[#id="search__input"] XPath locator, while you need the second one.
You have to use unique locator.
This should work better:
text_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='header__search']//*[#id='search__input']")))
text_input.click()
text_input.send_keys("Anthony Albanese")
This can also be done with CSS Selectors. They are shorter in this case:
text_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".header__search #search__input")))
text_input.click()
text_input.send_keys("Anthony Albanese")
UPD
This is the code I used, exactly accordingly to what I wrote before:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)
url = "https://www.theaustralian.com.au/"
driver.get(url)
text_input = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".header__search #search__input")))
text_input.click()
text_input.send_keys("Anthony Albanese")
The web page after the code above looks as following:
And if you add an Enter click to the search input as following:
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)
url = "https://www.theaustralian.com.au/"
driver.get(url)
text_input = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".header__search #search__input")))
text_input.click()
text_input.send_keys("Anthony Albanese" + Keys.ENTER)
the web page will finally become as following

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

click function not rendering the page

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

Selenium is unable to locate elements by class name

I am trying to get a list of the prices from this page.
The class name of the elements I'm trying to get is called s-item__price.
This is my code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
url = 'https://www.ebay.de/sch/i.html?_from=R40&_nkw=iphone+8+&_sacat=0&LH_TitleDesc=0&LH_ItemCondition=3000&rt=nc&LH_Sold=1&LH_Complete=1'
chrome_options = Options()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(options=chrome_options)
browser.get(url)
print(browser.find_elements_by_class_name('s-item__price'))
browser.quit()
The output is just an empty list.
You can use WebDriverWait to wait until the javascript generated the element:
wait = WebDriverWait(browser, 15) # 15 sec timeout
wait.until(expected_conditions.visibility_of_element_located((By.CLASS_NAME, 's-item__price')))
You could also use presence_of_elements_located but if it comes to click interaction it won't work with hidden elements.
So prefer using: visibility_of_element_located
Example Code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
url = 'https://www.ebay.de/sch/i.html?_from=R40&_nkw=iphone+8+&_sacat=0&LH_TitleDesc=0&LH_ItemCondition=3000&rt=nc&LH_Sold=1&LH_Complete=1'
options = Options()
options.add_argument('--headless')
browser = webdriver.Chrome(options=options)
browser.get(url)
wait = WebDriverWait(browser, 15) # Throws a TimeoutException after 15 seconds
wait.until(expected_conditions.visibility_of_element_located((By.CLASS_NAME, 's-item__price')))
# you may also culd use the return value of the wait
print(browser.find_elements_by_class_name('s-item__price'))
browser.quit()
You get an empty list I think it because you need wait.
Use WebDriverWait and utilize .presence_of_all_elements_located to collect elements in a list.
Then extract them with a loop and you must call the .text method to grab the text
browser.get('https://www.ebay.de/sch/i.html?_from=R40&_nkw=iphone%208%20&_sacat=0&LH_TitleDesc=0&LH_ItemCondition=3000&rt=nc&LH_Sold=1&LH_Complete=1')
wait = WebDriverWait(browser, 20)
list_price = wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, 's-item__price')))
for price in list_price:
print(price.text)
driver.quit()
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Click function doesn't work on elements for voting

I'm experimenting witch selenium in Python. I'm trying to click at up or down vote button below the comment. I'm using XPath to determinate specyfic button. There's no error occured but counter doesn't increase after clicking. I have tried on different webpages but results are same.
My first approach was that, I have used find_element_by() function but after that I could't use click() method on returned element. Now I'm useing ActionChains
This is my script
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Firefox()
driver.get("https://forsal.pl/praca/wynagrodzenia/artykuly/1422953,nik-w-nbp-sa-nieprawidlowosci.html")
driver.maximize_window()
wait = WebDriverWait(driver,30)
action = ActionChains(driver)
cookieButton = wait.until(EC.element_to_be_clickable((By.ID,"inforcwp-y")))
cookieButton.click()
time.sleep(5)
#wait.until(EC.visibility_of((By.XPATH,"/html/body/div[2]/section/div[2]/div[1]/div[1]/div[1]/div/div[9]/div[2]/div/ul/li[20]/p[1]/span[4]/a[2]")))
element = driver.find_element(By.XPATH,"/html/body/div[2]/section/div[2]/div[1]/div[1]/div[1]/div/div[4]/div[2]/div/ul/li[8]/p[1]/span[4]/a[2]")
element.location_once_scrolled_into_view
time.sleep(5)
action.double_click(element)
time.sleep(5)
driver.quit()
I'm expecting to increase up/down vote cunter after clicking on "voting hand"
Please give mo some advices how to achive my goal
To click() on the upvote icon 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.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("start-maximized")
chrome_options.add_argument('disable-infobars')
driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://forsal.pl/praca/wynagrodzenia/artykuly/1422953,nik-w-nbp-sa-nieprawidlowosci.html")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID,"inforcwp-y"))).click()
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,"//span[#class='headerUnderline' and contains(., 'Komentarze')]"))))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//ul[#id='commentsList']/li/p//span[#class='kf-rating']//a[#class='ratingUp']"))).click()

Categories