Element Exception - python

I am trying to execute this code and it shows 'NoSuchElementException' this error.
So can anyone help me for this ?
Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver= webdriver.Chrome()
page=driver.get("http://www.awgp.org")
banner_tell=driver.find_element_by_Link_text('Tell Me More')
banner_tell.click()

I think all you need to do is give link text in uppercase but this link is dynamic as the banner auto-slides to the next one.
You should come up with another locator to click on exactly the locator you want to click. Otherwise you might get ElementNotVisibleException if the banner is changed.
banner_tell=driver.find_element_by_link_text('TELL ME MORE')

try with xpath
banner_tell= driver.find_element_by_xpath("//*[contains(text(),'TELL ME MORE')]")

Seems you were almost near however the function() should have been :
find_element_by_link_text()
To click on the button with text as TELL ME MORE you have to induce WebDriverWait with expected_conditions clause element_to_be_clickable as follows :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "TELL ME MORE"))).click()

Related

Selenium Python find_element Xpath cant find xpath

I want to access an website with selenium and than a addblock-window appears, in which i need to click a button for it to disappear.
Eventhough I can find my XPath(//button[#title='Einverstanden'], /html/body/div/div[2]/div[3]/div[1]/button[#title = 'Einverstanden'] or
//button[contains(text(),"Einverstanden")]') in the browser,
I can't find it with my Python script. And i can't seem to find the mistake.
Here is my 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
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.get("``https://www.derstandard.at/story/2000134260361/endspiel-vor-gericht-prozess-gegen-boris-becker-startet-in-london``")
driver.maximize_window()
x = driver.find_element(By.XPATH, "//button[#title = 'Einverstanden']")
print(x)
This is the error I'm getting.
The button is enclosed in an iframe in which case, you first need to switch to the iframe and then access the element
This should work:
driver.get("https://www.derstandard.at/consent/tcf/")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//div[contains(#id, 'sp_message_container')]//iframe")))
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[title='Einverstanden']"))).click()
Wait Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
In case you want to switch to default frame again, you may use this when required:
driver.switch_to.default_content()

How to select a textbox using selenium in python

HTMLI want to select a textbox using XPath or any other locator, but I am unable to do so. The code is working fine for one part of the page, while it is not working using any locator for the other half of the page. I am not sure if my code is wrong or if something else is the problem.
I have attached the HTML part.
Here is my code:
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
driver.get('Website')
driver.implicitly_wait(50)
driver.find_element_by_xpath('//*[#id="j_username"]').send_keys("Username")
driver.find_element_by_xpath('//*[#id="j_password"]').send_keys("Password")
driver.find_element_by_xpath('//*[#id="b_submit"]').click()
driver.find_element_by_xpath('//*[#id="15301"]/div[1]/a/span').click()
driver.find_element_by_xpath('//*[#id="22261"]/a').click()
driver.find_element_by_xpath('//*[#id="22323"]/a').click()
driver.implicitly_wait(50)
driver.find_element_by_xpath('//*[#id="filterRow"]').clear()
The last line is where I am getting the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="filterRow"]"}
Page may have not finished rendering when you try to find the element. So it will give NoSuchElementException
Try the following method
elem = driver.find_element_by_xpath('//*[#id="filterRow"]')
if len(elem) > 0
elem[0].clear()
Hope this will help you
You can wait till the elements loads using wait -
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
Filter_Row = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="filterRow"]')))
Filter_Row.clear()
Try the above code and see what happens.
Try below solution
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[#id='filterRow']"))).clear()
Note: add below imports to your solution :
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
As in one of comments, you mentioned upon clicking tab a new page is opening. Can you please check if its opening in a new frame. Is so please switch to frame first where your element is using below statement:
driver.switch_to.frame(driver.find_element_by_name(name))
To navigate back to original frame you can use:
driver.switch_to.default_content()
using 'find_element_by_css_selector'
driver.find_element_by_css_selector("input")

How to create an automation for tplink pharos cpe520 using xpath with selenium and python for log in?

I'm trying to create an automation for my tplink pharos cpe520
This is the full xpath
"/html/body/div[4]/div/div[4]/div/div/div/div/div[1]/div[2]/div[1]/div[2]/div[2]/div[2]/div[1]/span[2]/input" it never change. I have to use xpath because every time the id is changed.
this is the xpath
//*[#id="widget--95952b3d-c134-3cfe-dd46-1a85b70c6882"]/div[2]/div[1]/span[2]/input
this is a new xpath
//*[#id="widget--059a411f-7134-3cfe-ec40-3c71bd80af37"]/div[2]/div[1]/span[2]/input
as you can see it changed
1
Try below xpath :
driver.find_element_by_xpath("//input[#type='text']")
or
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#type='text']']"))).send_keys("Test")
Note : add below impports to your solution
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
The problem was solved with "implicitly_wait(5)" before I get the link. It was not a problem from xpath.

Login Button can not be found with selenium

https://www.sevenonemedia.de/tv/programm/programmwochen
Here I want to login:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
options = Options()
chrome_path = "T:/Markus/WebScrapingExample/Chromedriver/chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_path,chrome_options=options)
driver.set_window_size(1280, 720)
time.sleep(5)
driver.get("https://www.sevenonemedia.de/tv/programm/programmwochen")
driver.find_element_by_id("_58_login").send_keys("name")
driver.find_element_by_id("_58_password").send_keys("pw")
driver.find_element_by_xpath('//*[#id="sign-in-button"]').click()
ElementNotInteractableException: element not interactable
(Session info: chrome=78.0.3904.97)
This is my error
Id is there. Why does this happen?
This page contains duplicate of element with ID sign-in-button. If your selector points to more than one element, driver always takes the first from the top of the DOM one which is not interactable in this case. You must refer to the second element with this id. Try this selector for "Sign in" button:
//*[#id="aheadcustom_p_p_id_58"]//button
hi first things you should not give your password to all of the stackoverflow community :)
you can't click on the button because there is a popup at the bottom of the page and you have to click on it first for selenium it's hidding your button
last is that the totality of your code ?
if yes you forgot
driver = webdriver.Firefox() #or any other webdriver
you have not create driver without this line
EDIT !!
it wasn't working with only the modification above but with this one its good
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
time.sleep(5)
driver = webdriver.Firefox()
driver.get("https://www.sevenonemedia.de/tv/programm/programmwochen")
driver.find_element_by_id("_58_login").send_keys("login")
driver.find_element_by_id("_58_password").send_keys("pssd")
driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/a").click()
driver.find_element_by_css_selector("#_58_fm > fieldset:nth-child(1) > div:nth-child(6) > button:nth-child(1)").click()
this work :)
sometime css selector a safer and work better
To click on the Login button you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using XPATH:
driver.get("https://www.sevenonemedia.de/tv/programm/programmwochen")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#title='Anmelden' and not(contains(#name,'INSTANCE'))]"))).send_keys("a.mai#mediaplus.com")
driver.find_element_by_xpath("//input[#title='Passwort' and not(contains(#name,'INSTANCE'))]").send_keys("Edidaten17")
driver.find_element_by_xpath("//input[#title='Passwort' and not(contains(#name,'INSTANCE'))]//following::button[1]").click()
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
Browser Snapshot:
The problem is that your button locator isn't unique on the page. It finds two buttons, the first of which is not visible which causes the ElementNotInteractableException.
The simple fix is to use the CSS selector below
#main-content #sign-in-button
That will find only the button you want. So your last few lines of code would be
driver.find_element_by_id("_58_login").send_keys("name")
driver.find_element_by_id("_58_password").send_keys("pw")
driver.find_element_by_css_selector('#main-content #sign-in-button').click()
sleep(1)
login_box = driver.find_element_by_name('login')
login_box.click()
this is for facebook login button, just inspect the website and see the id/name/type to make your code automate to work properly

How to create Xpath for clickable text in Selenium Webdriver (Python)?

I am working with this code code:
I am trying to find text "Branch Selection" within a web page to click on it.
So I do this:
driver.find_elements_by_xpath("//*[contains(text(), 'Branch Selection')]").click()
It doesn't throw an error, but the click doesn't happen. What am I doing wrong?
If it clicks but nothing happens, first try adding a sleep like sleep(5) to help debug before clicking to see if its because Selenium thought the page loaded when it actually didn't finish loading. If you can click after sleep of 5 seconds then you need to use WebDriverWait and EC as #DebanjanB had shown. In worst case scenario you'll have to use a sleep in your code but try to get the sleep to as short as possible.
Otherwise you might have multiple elements on the page with Branch Selection text such as in the meta tags. Try using XPATH example below to isolate the XPATH look up:
"//button[.//*[contains(text(), 'Branch Selection')]]"
or if there's more than one phrase on page containing the text, use following to select exact text
"//button[.//*[text()='Branch Selection']"
This selects the button element that has a child element with the text you're looking for. More XPATH details here: https://devhints.io/xpath
As per the HTML provided to click on the element with text as Branch Selection you need to induce WebDriverWait for the element to be clickable as follows :
CSS_SELECTOR :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.item.item-block.item-md div.input-wrapper>ion-label.label.label-md[id^=lbl-]"))).click()
XPATH :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='item item-block item-md']//div[#class='input-wrapper']/ion-label[#class='label label-md' and starts-with(#id,'lbl-') and contains(.,'Branch Selection')]"))).click()

Categories

Resources