In a Browser I have couple of img id's which shall dynamically change in every different selection of the release date.
Can you please guide me with a selenium web driver code which shall click the checkbox image, even if the image id changes over every different selection of the release date.
How to retrieve all the img id's in a web browser?
<img id="R1410ENDec14001001" class="child-img" src="../dyn/assets/checkbox_unchecked.png">
You can go with class or other properties if the id is dynamic like
//img[#class='child-img'] or
//img[contains(#src,'checkbox_unchecked.png')]
To retrieve all the image's which has id you can go with the following xpath
//img[#id]
Use findElements By xpath using the above one, and iterate over and get the id atribute of each element
Related
I'm not able to retrieve some elements present in the HTML file inspected in the browser, because some elements in the html file displayed by the driver have different class names, from the ones that are present in the browser html inspection.
The page doesn't have iframe elements and also selenium is not able to find this elements even with the WebDriverWait(driver,20).until(EC.presence_of_element_located((By.CLASS_NAME,"name of the class"))) function. I've tried to access also the inner and the outer html but the result does not change.
I think that the problem relies on the fact that the page dynamically loads the content and because of that after it is loaded the elements class name change dynamically.
Unfortunatly I can't show the HTML code because it contains private information.
I was working to scrape links to articles on a website. But normally when site was loaded it list only 5 articles then it requires to click load more button to display more articles list.
Html source has only links to first five articles.
I used selenium python to automate clicking load more button to completely load webpage with all article listings.
Question is now how can i extract links to all those articles.
After loading site completely with selenium i tried to get html source with driver.page_source and printed it but still it has only link to first 5 articles.
I want to get links to all those articles that were loaded in webpage after clicking load more button.
Please someone help to provide solution.
Maybe the links take some time to show up and your code is doing driver.source_code before the source code is updated. You can select the links with Selenium after an explicit wait so that you can make sure that the links that are dinamically added to the web page are fully loaded. It is difficult to boil down exactly what you need without a link to your source, but (in Python) it should be something similar to:
from selenium.webdriver.support.ui import WebDriverWait
def condition(driver):
"""If the selector defined in the function retrieves 10 or more results, return the results.
Else, return None.
"""
selector = 'a.my_class' # Selects all <a> tags with the class "my_class"
els = driver.find_elements_by_css_selector(selector)
if len(els) >= 10:
return els
# Making an assignment only when the condition returns a truthy value when called (waiting until 2 min):
links_elements = WebDriverWait(driver, timeout=120).until(condition)
# Getting the href attribute of the links
links_href = [link.get_attribute('href') for link in links_elements]
In this code, you are:
Constantly looking for the elements you want until there are 10 or more of them. You can do this by CSS Selector (as in the example), XPath or other method. This gives you a list of Selenium objects as soon as the wait condition returns an object with a True value, until a certain timeout. See more on explicit waits in the documentation. You should make the appropriate condition for your case - maybe expecting a certain number of links is not good if you are not sure of how many links there will be in the end.
Extracting what you want from the Selenium object. For that, use the appropriate method over the elements in the list you got from the step above.
I am beginner and using xpath and other locators like name, ID and so on for finding web elements in Web page. Just want to know why we need locators to find out elements.
Thanks
The locators are used in order to interact with the web pages by uniquely identifying elements. However, it is considered best practice to add data attributes specific for the test automation to the web pages, such as data-testid in place of using xpath, css selectors, etc.
Locators are used in order to interact with the webelements available on the page.
For ex. If I want to enter any text in the input field, first I would have to find out location of that input field on the web page and then only I would be able to enter text into it.
Can you help me to identify element ID or any other locator of timeline composer in Facebook profile ?
I need this to use in Robot framework with selenium2library to post something on my wall.
I can log in to Facebook, navigate to profile, but I cant input text into timeline composer. I tried to use Click element before inserting text, but no success.
I am using "inspect element" in browsers/firebug add-on to identify elements.
In this case, unfortunately all locators I have tried giving errors like:
Element does not appear in 5 seconds
or
Element must be user editable in order to clear it
Non dynamic locator for FB timeline-composer has name "xhpc_message_text" (18.10.2016)
Input text name=xhpc_message_text test
Does Selenium/Protractor look for element in current loaded page, or look in the entire application when using the same Css Selector for same elements?
Eg:
Save Button on Customers Form class="save"
Save Button on Vendors Form class="save"
Protractor works by interacting with the browser (via selenium). Selenium uses browser drivers to interact with your page, and the browser only contains the code that it asked for (returned from the server, based on the type of request that was made).
So yes, it only looks for elements in the currently loaded page. It has no access to your entire application code.
Selenium looks for the element only on the loaded page. Not sure about Protractor though.