for i in range(len(rows)):
columns = rows[i].find_element(by=By.TAG_NAME, value="td")
for j in range(len(columns)):
if columns[j].text == "JKL":
columns[0].click()
What is the problem of my code?
I think I see the issue, in this line columns = rows[i].find_element(by=By.TAG_NAME, value="td")
you're only getting one element so it can't loop through it. Essentially it's saying columns = one column element with these attributes. .find_element()
is the issue since it only finds one element.
The source of the error is that find_element()
is returning a WebElements
type. len()
generally doesn't work on a webelement so you get an error.
If you were trying to create a list of rows which have the <td>
tag then you would start by declaring the list as columns = []
just before the for i
loop. The line to build the list would look like this:
columns.append(rows[i].find_element(by=By.TAG_NAME, value="td"))
which would give you a list of webelements.
That nested for j
loop is probably not necessary; you can probably just compare each tag as you extract it from rows
, but I think that find_element()
is going to throw an exception on every line which does not have the tag. I'm guessing that you should probably be looking for an <a>
tag rather than <td>
. I don't have enough experience with Selenium to speak more competently, but I suspect that you're probably looking for something more along the lines of this:
for i in range(len(rows)):
try:
column = rows[i].find_element(by=By.TAG_NAME, value="td")
except NoSuchElement:
#we expect this exception; do nothing
pass
else:
if column.text == "JKL":
column.click()
break
The break
should get you out of the for
loop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.