Selenium Python Xpath Element Unable To Locate Element

Find the data you need here

We provide programming data of 20 most popular languages, hope to help you!

Previous PostNext Post

Unable to locate element by xpath using Selenium with Python

What you are likely looking for is the input element instead, which is nested inside of that div element. element = …

# FIRST I OPEN INSTAGRAM LIKE THIS
self.driver.get("https://www.instagram.com/")

# THEN A POP-UP APPEARS, AND I CLOSE IT BY CLICKING IN THE ACCEPT BUTTON,
# IT SEEMS LIKE IT IS NOT AN IFRAME SO NO FRAME SWITCHES ARE NEEDED
cooki = self.driver.find_element_by_xpath('/html/body/div[2]/div/div/div/div[2]/button[1]')
cooki.click() 
 
# NOW I TRY TO FIND THE USERNAME FORM TO WRITE IN AN INPUT
usbar = self.driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[1]')    # ERROR HERE
usbar.send_keys(input('Username: '))
element = self.driver.find_element_by_xpath('//*[@name="username"]')
element.send_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 for the element to appear in the HTML for a maximum of 10 seconds
element = WebDriverWait(self.driver, 10).until(
    EC.presence_of_element_located((By.NAME, "username"))
)

# Send input to the element
element.send_keys('...')
usbar = driver.find_element_by_xpath('/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input') 
...

######################################## TL;DR

firefoxOpt = webdriver.FirefoxOptions()
firefoxSrv = FirefoxService(GECKODRV_PATH)
firefoxSrv.creationflags = CREATE_NO_WINDOW
profile = webdriver.FirefoxProfile()
...

drv = webdriver.Firefox(executable_path=GECKODRV_PATH,
                        service=firefoxSrv,options=firefoxOpt,firefox_profile=profile)

cookies = pickle.load(open("COOKIE_PATH", "rb"))
drv.get('TARGET_URL')        # Should be there for cookie injection to work

for cookie in cookies:
    drv.add_cookie(cookie)

drv.get(TARGET_URL)
time.sleep(10)

drv.find_element(By.XPATH, XPATH_ITEM_TO_CLICK).click()   # Works fine,no worries
time.sleep(10)

#########################################

# This is what plagues me
print(drv.find_element(By.XPATH,
      "//*[@id=\"modern-flyout\"]/div/div[5]/div/div/div[1]/div/div").text)

Python/Selenium – “no such element: Unable to locate element”

But methods to locate elements on the page that worked for me in the past, have been fruitless with this site. Here’s some sample code outlining my issue. 9. 1. from selenium import webdriver. 2. 3. driver = webdriver.Chrome(executable_path= "C:/work/chromedriver.exe") 4.

from selenium import webdriver

driver = webdriver.Chrome(executable_path= "C:/work/chromedriver.exe")
driver.get('https://www.weforum.org/reports/global-gender-gap-report-2021/in-full/economy-profiles#economy-profiles')

# click the `list` view option
driver.find_element_by_xpath('//*[@id="root"]/div/div[1]/div[2]/div[2]/svg[2]')

driver.get("https://www.weforum.org/reports/global-gender-gap-report-2021/in-full/economy-profiles#economy-profiles")
wait=WebDriverWait(driver,10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iFrameResizer0")))
wait.until(EC.element_to_be_clickable((By.XPATH,"//*[name()='svg' and @class='sc-gxMtzJ bRxjeC']"))).click()
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver.get("https://www.weforum.org/reports/global-gender-gap-report-2021/in-full/economy-profiles#economy-profiles")
wait=WebDriverWait(driver,10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iFrameResizer0")))
driver.find_element_by_xpath("//*[name()='svg' and @class='sc-gxMtzJ bRxjeC']").click()

Unable to Locate Element with Selenium Python despite xPath

the xpath explanation is basically, we are targeting the input tag using //input and then we are trying to filter with the name attribute and class attributes, and we are using contains to look for specific text in class. for this it turned out that element on the website is correctly identified.Could you please explain, - I would have to see the HTML structure at least to have …

html body div.main-content section.section div.block-one_col_1 div.container-fluid.wrapper div.row div.col-12 form#id_form-process.form-horizontal.formfactory div.row div.col-md div#fieldgroup_variants.fieldset div#fieldgroup_variants__fields.fieldset__content div.form-group.row.formfield_variantsCalculator.fieldpk_2533.mb-0.is-valid div.col-md-6.col-12 div.d-flex.flex-row div.w-100 div.table-scroll table.table.table--step tbody#tableContent.table tr.table-row.table-row--count td div.form-group.group1 input.form-control.variant-value.variant1.touched
/html/body/div/section/div/div/div/div/form/div[1]/div/div[4]/div[2]/div/div/div/div[1]/div[1]/table/tbody/tr[1]/td[3]/div/input
<td data-risk="6.1"><div class="form-group group1" data-excluded="false" data-risk="6.1"><input name="1" class="form-control variant-value variant1 touched" data-editable="true" data-risk="6.1" data-visible-sum="true" data-dynamic="false"></div></td>
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@name= '1' and contains(@class, 'variant-value')]"))).send_keys('10000')
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
 raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="mount_0_0_/O"]/div/div[1]/div/div[4]/div/div/div[1]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[2]/div[1]/div[1]/span/text()"}
  (Session info: chrome=97.0.4692.71)
# imports
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
# from selenium.webdriver.common.keys import Keys
import time
# import csv

# Exceptions to handle unexpected faults!!!
# from selenium.common.exceptions import WebDriverException
# from selenium.common.exceptions import NoSuchElementException
# from selenium.common.exceptions import NoSuchFrameException
# from selenium.common.exceptions import NoSuchWindowException
# from selenium.common.exceptions import ElementNotInteractableException
# from selenium.common.exceptions import StaleElementReferenceException
# from selenium.common.exceptions import MoveTargetOutOfBoundsException
# from selenium.common.exceptions import UnexpectedAlertPresentException
# from selenium.common.exceptions import ElementClickInterceptedException
# from selenium.common.exceptions import JavascriptException


chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)

driver = webdriver.Chrome('F:/Real-Trader/chromedriver', chrome_options=chrome_options)

driver.get("http://www.facebook.com")

alert=WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Az összes cookie engedélyezése")]'))).click()
username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']")))
password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='pass']")))

username.clear()
username.send_keys("@@@@@@@@@@@@@")
password.clear()
password.send_keys("*************")

button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
 
time.sleep(5) 

driver.get("https://www.facebook.com/marketplace/109375022413858/propertyforsale/")
time.sleep(5)
    
for j in range(0,0):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(5)

anchors = driver.find_elements_by_tag_name('a')
anchors = [a.get_attribute('href') for a in anchors]
   
anchors = [a for a in anchors if str(a).startswith('https://www.facebook.com/marketplace/item/')]

links = []

for a in range(len(anchors)):
    #print('------------------------')
    the_link = str(anchors[a])
    character_cut = int(anchors[a].find('/?'))
    cut_link = the_link[:character_cut]
    #print(cut_link)
    links.append(cut_link)
    
ad = []

for elem in links:
    print('------------------------')
    print(elem)
    driver.get(elem)
    time.sleep(5)
    
# This is, what i should use succesfully....!
    title = driver.find_element_by_xpath('//*[@id="mount_0_0_/O"]/div/div[1]/div/div[4]/div/div/div[1]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[2]/div[1]/div[1]/span/text()').text
    location = driver.find_element_by_xpath('//*[@id="mount_0_0_/O"]/div/div[1]/div/div[4]/div/div/div[1]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[2]/div[1]/div[7]/div[3]/div/div[1]/span/text()').text
    price = driver.find_element_by_xpath('//*[@id="mount_0_0_/O"]/div/div[1]/div/div[4]/div/div/div[1]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[2]/div[1]/div[1]/div[1]/div/span/text()').text
    button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='Tovább']"))).click()
    description = driver.find_element_by_xpath('//*[@id="mount_0_0_/O"]/div/div[1]/div/div[4]/div/div/div[1]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[2]/div[1]/div[8]/div[2]/div/div/div/span/text()').text
    seller_name = driver.find_element_by_xpath('//*[@id="mount_0_0_/O"]/div/div[1]/div/div[4]/div/div/div[1]/div/div[3]/div[2]/div/div[2]/div/div[2]/div/div[2]/div[1]/div[9]/div/div/div[2]/div/div/div/div/div[2]/div/div/div/div[1]/span/span/div/div/div/span').text
    button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='Eladó adatai']"))).click()
    # seller_data = driver.find_element_by_xpath().text
    
# This is, what is actually working... :(
    # description = driver.find_element_by_tag_name('div')
    # print(description.text)
    # ad.append(description)

Xpath is Unable to Locate Element.

You can copy the xpath from the Chrome/Firefox inspect elements: 1- Put the mouse on the element you want to inspect 2- Right click, Inspect element 3- Down bar shows, Right clikc, copy, copy xpath Done!

//ul[@id="dropdown1"]/preceding-sibling::a
//a[@class='dropdown-button btn'];
//*[contains(@class,'active')]/descendant::a[@class='dropdown-button btn']
1- Put the mouse on the element you want to inspect
2- Right click, Inspect element
3- Down bar shows, Right clikc, copy, copy xpath

Unable to locate an element using xpath error in selenium-java

How to select element using XPATH syntax on Selenium for Python? Find Element and FindElements by XPath in Selenium; How to locate element by partial id match in Selenium? Which exception is raised when an element is not found in an HTML DOM using XPath in Selenium? Clicking on elements within an SVG using XPath (Selenium …

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class XpathError{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
         "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      //launch URL
      driver.get("https://www.tutorialspoint.com/index.htm");
      WebElement m = driver.findElement(By.xpath("//*[text()='Library']"));
      m.click();
      //explicit wait condition - visibilityOfElementLocated
      w.until(ExpectedConditions.visibilityOfElementLocated (By.linkText("Subscribe to Premium")));
      System.out.println("Page title: " + driver.getTitle());
      driver.quit();
   }
}

Previous PostNext Post

Related code examples