No such Element Exception using selenium in python

2024/7/4 15:55:49
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chrome_path=r"C:\Users\Priyanshu\Downloads\Compressed\chromedriver_win32\chromedriver.exe"
driver=webdriver.Chrome(chrome_path)
driver.get("https://www.flipkart.com/?")
search = driver.find_element_by_name('q')
search.send_keys("laptop")
search.send_keys(Keys.RETURN)
driver.find_element_by_xpath(""" //*[@id="container"]/div/div[2]/div[2]/div/div[2]/div[2]/div/section/ul/li[2]""").click()

I am getting no such element present in the last line of code.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chrome_path=r"C:\Users\Priyanshu\Downloads\Compressed\chromedriver_win32\chromedriver.exe"
driver=webdriver.Chrome(chrome_path)
driver.get("https://www.flipkart.com/search?q=laptop&otracker=start&as-show=off&as=off")driver.find_element_by_xpath(""" //*[@id="container"]/div/div[2]/div[2]/div/div[2]/div[2]/div/section/ul/li[2]""").click()

If I am doing like this its working fine.

Answer

The element is not immediately available, wait for it to be present first:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ECwait = WebDriverWait(driver, 10)search = wait.until(EC.presence_of_element_located((By.NAME, 'q')))
search.send_keys("laptop")
search.send_keys(Keys.RETURN)element = wait.until(EC.presence_of_element_located(By.XPATH, '//*[@id="container"]/div/div[2]/div[2]/div/div[2]/div[2]/div/section/ul/li[2]'))
element.click()

Note that, assuming you want to get to the "Popularity" menu header, why don't simplify the XPath expression and use the element's text:

//li[. = "Popularity"]
https://en.xdnf.cn/q/119761.html

Related Q&A

Web scraping, cant get the href of a tag

Im trying to scrape this Page https://rarity.tools/thecryptodads Using Selenium in python. At the top of the right of each card below, theres the owner name that contains a link once pressed, it takes …

Using Python Pandas to fill new table with NaN values

Ive imported data from a csv file which has columns NAME, ADDRESS, PHONE_NUMBER. Sometimes, at least 1 of the columns has a missing value for that row. e.g0 - Bill, Flat 2, 555123 1 - Katie, NaN, NaN 2…

sympy AttributeError: Pow object has no attribute sin

I have read this SO post which says namespace conflict is one reason for this error. I am falling to this error frequently. So, Id like to learn what exactly is happening here? What is expected by the…

Tkinter unbinding key event issue

In the code below, pressing the space bar twice results in two successive beeps. I want to avoid this and instead disable the key while the first beep is happening. I thought unbinding the space key mi…

Is there a way to find the largest change in a pandas dataframe column?

Im trying to find the largest difference between i and j in a series where i cannot be before j. Is there an efficient way to do this in pandas:x = [1, 2, 5, 4, 2, 4, 2, 1, 7] largest_change = 0for i i…

Updating scikit-learn to latest version with Anaconda environment fails with http error 000

I use Anaconda3 installed on my pc Win10 64bits. I noticed it runs with an outdated scikit learn version (0.21.3), and I am trying to update it (0.24.1 available on https://repo.anaconda.com/pkgs/main/…

Python RegEx remove new lines (that shouldnt be there)

I got some text extracted and wish to clean it up by RegEx.I have learned basic RegEx but not sure how to build this one:str = this is a line that has been cut. This is a line that should start on a …

Python CSV writer

I have a csv that looks like this:HA-MASTER,CategoryID 38231-S04-A00,14 39790-S10-A03,14 38231-S04-A00,15 39790-S10-A03,15 38231-S04-A00,16 39790-S10-A03,16 38231-S04-A00,17 39790-S10-A03,17 38231-S04-…

How to perform standardization on the data in GridSearchCV?

How to perform standardizing on the data in GridSearchCV?Here is the code. I have no idea on how to do it.import dataset import warnings warnings.filterwarnings("ignore")import pandas as pd …

how to find the permutations of string? python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…