I am trying to perform a web automatization with python and selenium in chrome.
The thing is im trying to locate a button which has not id or class name.
The xpath is:
//*[#id="Form1"]/table[1]/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td/span[1]
And the html code is
<span class="SectionMethod" onclick="window.location.href="explorer/explorer.aspx?root=user";" style="cursor:pointer;text-decoration:underline;color:CadetBlue;">Open</span>
It's a button called open, but there are other buttons like that one with the same text and class so i cant locate by text.
This is my code:
from selenium import webdriver
driver = webdriver.Chrome(chrome_options=chromeOptions, desired_capabilities=chromeOptions.to_capabilities())
driver.get("..............")
driver.find_element_by_xpath('//*[#id="Form1"]/table[1]/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td/span[1]')
This is the error i am getting:
NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"Form1"}
(Session info: chrome=75.0.3770.100)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Windows NT 10.0.16299 x86_64).
It's likely you are looking for an element before it has loaded. As per the example from the documentation
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("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
In your case:
EC.presence_of_element_located((By.ID, "myDynamicElement"))
Will be
EC.presence_of_element_located((By.XPATH, '//*[#id="Form1"]/table[1]/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td/span[1]'))
If that doesn't fix the error I suggest you review how to form a MCVE, and how to ask a well received question (recommended reading when creating a new account). Then edit your question into a more concise format, so we can more effectively help you! Welcome to StackOverflow.
Your correct xpath is:
//span[#class='SectionMethod' and text() = 'Open']
Presumably you are trying to click() on the <span> element with text as Open and to achieve that you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.SectionMethod[onclick*='explorer/explorer']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='SectionMethod' and contains(#onclick,'explorer/explorer')][text()='Open']"))).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
Additionally, as you are using chrome=75.0.3770.100 you need to update ChromeDriver to ChromeDriver 75.0.3770.90 (2019-06-13)
Related
I'm trying to make a simple script that goes to the homepage of The Whiskey Exchange, clicks on the following menu item, navigates to the new page and finally grabs a screenshot.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
chrome_options = Options()
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.thewhiskyexchange.com/")
driver.implicitly_wait(3)
search_bar = driver.find_element(
By.XPATH, "/html/body/div[3]/nav/div/div[3]/div/div/div[2]/a[2]")
search_bar.click()
driver.implicitly_wait(3)
driver.save_screenshot('./image.png')
driver.quit()
I've tried multiple approaches to get this to work, mainly by changing the type of finder, but with no luck. I've tried copying both XPATH and Full XPATH (directly from chrome), but that hasn't worked either. Maybe it's something to do with how the site is built, but no idea.
I'm getting the following error message:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/nav/div/div[3]/div/div/div[2]/a[2]"}
First time using selenium, so the struggle is a bit real. Please help.
To expand the Scotch Whisky menu you don't need to click on the item, instead you can simply Mouse Hover and grab a screenshot of the webpage using either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://www.thewhiskyexchange.com/')
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[title='Scotch Whisky']")))).perform()
driver.save_screenshot('./Scotch_Whisky.png')
driver.quit()
Using XPATH:
driver.get('https://www.thewhiskyexchange.com/')
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[#title='Scotch Whisky']")))).perform()
driver.save_screenshot('./Scotch_Whisky.png')
driver.quit()
Note : You have to add the following imports :
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Screenshot:
CSS_SELECTOR
a.header-button.header-button--search.js-header-button--search
XPATH
//a[#title='Search']
You could simply use this css selector since I don't see an xpath like that.
I want to use selenium to login in the website https://www.winamax.es/account/login.php?redir=/apuestas-deportivas. The case is that I don't find the xpath/id/text to get te next code running successfully:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
options = webdriver.ChromeOptions()
options.add_argument("window-size=1920,1080")
options.add_argument('--disable-blink-features=AutomationControlled')
driver=webdriver.Chrome(options=options,executable_path=r"chromedriver.exe")
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
WebDriverWait(driver=driver, timeout=15).until(
lambda x: x.execute_script("return document.readyState === 'complete'")
)
upload_field = driver.find_element_by_xpath("//input[#type='email']")
I don't only want the specific xpath for this example, but I prefer a method to obtain the xpath or something similar to get the code working for other parts of the website
<iframe id="iframe-login" data-node="iframe" name="login" scrolling="auto" frameborder="1" style="min-height: 280px; width: 100%;"></iframe>
Your element is in an iframe. Switch to it.
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iframe-login")))
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Full working code.
wait = WebDriverWait(driver, 10)
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iframe-login")))
upload_field = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#type='email']")))
upload_field.send_keys("stuff")
The email element 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 CSS_SELECTOR:
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#iframe-login")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='email']"))).send_keys("scraper#stackoverflow.com")
Using XPATH:
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='iframe-login']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#type='email']"))).send_keys("scraper#stackoverflow.com")
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:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
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
I am trying to get data from a password protected website with Selenium.
However I get stuck right in the beginning at login with the following error message:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="username"]"}
(Session info: chrome=87.0.4280.66)
The name I use is correct for sure, I inspected the website. Including waiting time did not help either...
My code is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from getpass import getpass
driver = webdriver.Chrome()
driver.get("https://clearing.apcs.at/emwebapcsem/startApp.do")
print(driver.title)
print(driver.current_url)
# create an object for searchbox
username=driver.find_element_by_name("username")
password=driver.find_element_by_name("password")
# typte the input
username.send_keys("XXXXXX")
password.send_keys("XXXXXX")
driver.find_element_by_name('login').click()
Any suggestions would be appreciated.
To send a character sequence to the Benutzer and Passwort field as the elements are within an <frame> 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 CSS_SELECTOR:
driver.get('https://clearing.apcs.at/emwebapcsem/startApp.do')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame[title='menu']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.loginContentBoxInput[name='username']"))).send_keys("Endre")
driver.find_element_by_css_selector("input.loginContentBoxInput[name='password']").send_keys("Endre")
Using XPATH:
driver.get('https://clearing.apcs.at/emwebapcsem/startApp.do')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame[#title='menu']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='loginContentBoxInput' and #name='username']"))).send_keys("Endre")
driver.find_element_by_xpath("//input[#class='loginContentBoxInput' and #name='password']").send_keys("Endre")
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:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
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
I want to click on "new order" icon in mt4 web terminal using selenium module in python
This is the code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.mql5.com/en/trading")
new_order = driver.find_element_by_xpath('/html/body/div[3]/div[1]/a[1]/span[1]')
new_order.click()
And this is the error that I get:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/div[1]/a[1]/span[1]"}
(Session info: chrome=86.0.4240.198)
What is the correct way to locate that button, I searched and found some ways to locate elements for selenium but I couldn't get any of them work for me.
Looks like your page is dealing with iframes. So while the above answer has good practices, you also need to switch to the iframe:
driver.switch_to.iframe(self,frame reference)
Look for more details at https://www.techbeamers.com/switch-between-iframes-selenium-python/ or https://stackoverflow.com/a/24286392/1387701
The element with tooltip as New Order 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 the following xpath based Locator Strategies:
driver.get('https://www.mql5.com/en/trading')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='webTerminalHost']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Connect to an Account']//following-sibling::div[1]/span"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#title='New Order']/span"))).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
You can use a different xpath:
new_order = driver.find_element_by_xpath('//a[#title="New Order"]')
But I would suggest By, WebDriverWait, and expected_conditions:
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
import time
driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.mql5.com/en/trading")
time.sleep(5)
iframe = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//iframe[#id="webTerminalHost"]')))
driver.switch_to.frame(iframe)
new_order = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//a[#title="New Order"]')))
new_order.click()
I have a specific login button I can't access with Python Firefox Selenium. It's the login button on this webpage: https://schalter.asvz.ch/tn/lessons/39616
I'm running Ubuntu 16.04, Python 3.5.2, Firefox 65.0 and Selenium 3.141.
I tried several combinations of approaches I found here on stackoverflow including the following:
login = driver.find_element_by_xpath("//*[#class='btn btn-default ng-star-inserted']")
login = driver.find_element_by_xpath("//button[#class='btn btn-default ng-star-inserted']")
login = driver.find_element_by_class_name('btn btn-default ng-star-inserted')
login = driver.find_element_by_xpath("//*[contains(., 'Login')]")
login = driver.find_element_by_name('app-lessons-enrollment-button')
But none of them worked. Always resulting in:
NoSuchElementException: Message: Unable to locate element:
//*[#class='btn btn-default ng-star-inserted']
What is so different with this button? How can I make it work?
This error message...
NoSuchElementException: Message: Unable to locate element: //*[#class='btn btn-default ng-star-inserted']
...implies that the ChromeDriver was unable to locate the desired element through the locator you have used.
Virtually, your first two(2) locators were just perfecto.
However, the desired element is an Angular element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-default.ng-star-inserted[title='Login']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-default ng-star-inserted' and #title='Login']"))).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
Try the below option.
Login=driver.find_element_by_css_selector("button.ng-star-inserted")
Or try this
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'button.ng-star-inserted'))).click()
You need following imports for option 2.
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
Following xpath works just fine (tested using selenium java)
//button[#title='Login']
I was able to locate and click the button.
Try the below xpath:
xpath = "//button[#title='Login']"
element = driver.find_element_by_xpath(xpath);
element.click();