Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I find an input element with a placeholder in selenium?

In a webpage I have the following element:

<input placeholder="Search in your collabs" class="md-input" type="text">

which I try to select by the following piece of code (in python):

elem = browser.find_element_by_xpath("//input[@placeholder='Search in your collabs']")

but when running the code I get the following error:

NoSuchElementException: Message: Unable to locate element: //input[@placeholder='Search in your collabs']

The element I am looking for is inside an iframe which I selected before correctly (I did another find_element_by_xpath before that with some other attributes). In detail, what I did before was:

browser.switch_to_frame(browser.find_element_by_id("clb-iframe-workspace"));

So but why is this not working now?

like image 682
Alex Avatar asked Sep 14 '17 07:09

Alex


1 Answers

Using sleep (as menitioned in your comment) is a bad solution because it inevitably slows down your test execution and might fail from day to day. A better solution is using timeouts. A timeout waits up to a specified time span but continues as soon as possible (i.e. your expected condition is fulfilled).

You should either increase your implicit wait timeout for locating elements or use an explicit wait like e.g.

from selenium.webdriver.support import expected_conditions as expect
....
//wait up to 120sec, poll every sec
elem = WebDriverWait(browser, 120, 1).until(
        expect.visibility_of_element_located(
        (By.XPATH, "//input[@placeholder='Search in your collabs']")))

as explained here

like image 184
Würgspaß Avatar answered Nov 14 '22 23:11

Würgspaß