Unable to locate element in Python Selenium

2024/10/13 10:24:01

I'm trying to locate an element using python selenium, and have the following code:

zframe = driver.find_element_by_xpath("/html/frameset/frameset/frame[5]")
driver.switch_to.frame(zframe)
findByXpath("/html/body/form/table/tbody/tr/td[2]/label[3]").click()
element = driver.find_element_by_xpath("//*[@id='awdType']")

I'm getting the error that:

selenium.common.exceptions.NoSuchElementException: Message: no suchelement: Unable to locate element:{"method":"xpath","selector":"//*[@id='awdType']"} (Session info:chrome=59.0.3071.115)

Any ideas why it may not be able to locate this element? I used the exact xpath by copying it and also switched frames. Thanks!

Answer

The problem occurs because awdType is loaded by ajax or jquery. You should use selenium Waits. There is two type of waits explicit and implicit.Avoid using implicit wait.

# Explicit wait example
from selenium.webdriver.support import expected_conditions as ECwait = WebDriverWait(driver,20)
element = wait.until(EC.element_to_be_clickable((By.ID, 'awdType')))

OR

# implicit wait example
driver.implicitly_wait(10) # seconds
element = driver.find_element_by_xpath("//*[@id='awdType']")
https://en.xdnf.cn/q/118092.html

Related Q&A

How to import a variable from a different class

I have an instance of a class that i set the value to self.world inside a class named zeus inside a module named Greek_gods. and i have another class names World inside a module name World.How can i te…

Scrapy: AttributeError: YourCrawler object has no attribute parse_following_urls

I am writing a scrapy spider. I have been reading this question: Scrapy: scraping a list of links, and I can make it recognise the urls in a listpage, but I cant make it go inside the urls and save the…

initializer is not a constant, error C2099, on compiling a module written in c for python

i tried to compile a python module called distance, whith c "python setup.py install --with-c" using msvc 2017 on windows 10, i got this error ,Cdistance / distance.c (647): error C2099: init…

How can make pandas columns compare check cell?

I have a two file. a.txt has the below data.Zone,Aliase1,Aliase2 VNX7600SPB3_8B3_H1,VNX7600SPB3,8B3_H1 VNX7600SPBA_8B4_H1,VNX7600SPA3,8B4_H1 CX480SPA1_11B3_H1,CX480SPA1,11B3_H1 CX480SPB1_11B4_H1,CX480S…

Flask argument of type _RequestGlobals is not iterable

When I tried to use Flask-WTForms, I followed these steps:from flask_wtf import Form from wtforms import StringField, PasswordField from wtforms.validators import DataRequired, Emailclass EmailPassword…

PumpStreamHandler can capture the process output in realtime

I try to capture a python process output via apache-commons-exec. But it looks like it wont print the output, the output is only displayed after I the python process is finished.Heres my java codeComma…

Freezing a CNN tensorflow model into a .pb file

Im currently experimenting with superresolution using CNNs. To serve my model Ill need to frezze it first, into a .pb file, right? Being a newbie I dont really know how to do that. My model basically …

flattening a list or a tuple in python. Not sure what the error is

def flatten(t):list = []for i in t:if(type(i) != list and type(i) != tuple):list.append(i)else:list.extend(flatten(i))return listHere is the function that Ive written to flatten a list or a tuple that …

Counting word occurrences in csv and determine row appearances

I have a csv file such as the following in one column. The symbols and numbers are only to show that the file does not just contain text. I have two objectives:count the number of occurrences of a wo…

Converting Python 3.6 script to .exe? [duplicate]

This question already has answers here:How can I convert a .py to .exe for Python?(8 answers)Closed 6 years ago.I would like to convert a .py file to an .exe. I am using Python 3.6. I already tried py…