简体   繁体   中英

How to select element with selenium in python

I've tried manipulate some website with selenium. Unfortunately, I've encountered problem to go further.

After opening the website I need to select one element and execute click event. I've tried with find_element_by: name, tag_name, class_name, but every time I've received massage "There's no such element". Inspection of this element gives this output:

<div 
style="position: absolute; left: 90px; top: 3px; width: 48px; height: 18px; text-align: center; cursor: pointer; font-family: tahoma; font-size: 11px; font-weight: normal; border-top: 1px solid rgb(85, 85, 85); border-left: 1px solid rgb(85, 85, 85); border-right: 1px solid rgb(85, 85, 85); padding-top: 5px; background-color: rgb(117, 148, 159); border-radius: 4px 4px 0px 0px; color: rgb(255, 255, 255);"
>Działki</div>

How to select this element with selenium (python)

UPDATE:

I thought xpat is solution and partially that's true

Now I've tried this:

browser.find_elements_by_xpath("/html/body/div[3]/div[8]/div[2]/div[1]/div[4]").click()

... but now I've got this:

AttributeError: 'list' object has no attribute 'click'

What is wrong?

If you know what the div will contain but don't have a class or name, you could try to identify it by its internal contents. For your example that would be "Działki"

This answer explains how to do this: How do I find an element that contains specific text in Selenium Webdriver (Python)?

If you know the text in the div and it's unique you can locate it with text() .

It will look something like this:

browser.find_elements_by_xpath("//div[text()='Działki']")

Hope this helps you!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM