How to Use Robot Framework with Selenium to Find HREF Links Containing Specific Text
Автор: vlogize
Загружено: 2025-05-27
Просмотров: 0
Learn to locate all HREF links on a webpage containing specific text using `Robot Framework` and `Selenium`. We break down the solution to ensure you avoid common errors.
---
This video is based on the question https://stackoverflow.com/q/66615738/ asked by the user 'user3925023' ( https://stackoverflow.com/u/3925023/ ) and on the answer https://stackoverflow.com/a/66616011/ provided by the user 'Tarun Lalwani' ( https://stackoverflow.com/u/2830850/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Roboframework - Selenium - find all HREF containing specific text
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
How to Use Robot Framework with Selenium to Find HREF Links Containing Specific Text
When working with web automation using Robot Framework and Selenium, one common task is locating all the HREF links on a webpage containing certain text. However, you may stumble across errors if you incorrectly mix your selector types, such as CSS and XPath. In this post, we'll guide you through the correct methods for achieving your goal, ensuring smooth automation without running into frustrating errors.
The Problem: Invalid Selector Exception
Consider the following scenario where you are attempting to extract links from a webpage that contain the text photo/?fbid. You have tested your selector using Chrome DevTools, and it works as expected:
[[See Video to Reveal this Text or Code Snippet]]
However, when trying to implement the same selector in your Robot Framework script, you encounter an error:
[[See Video to Reveal this Text or Code Snippet]]
What Went Wrong?
The issue arises from mixing CSS and XPath selectors. You attempted to use a CSS selector ([href*='photo/?fbid']) but provided it as an XPath expression. This mismatch causes the invalid selector exception.
The Solution: Use the Correct Selector Syntax
To resolve this issue, you need to use the appropriate syntax for each selector type. Here are two correct methods you can use, one for XPath and one for CSS.
Method 1: Using XPath
If you prefer to use XPath, you can modify your script as follows:
[[See Video to Reveal this Text or Code Snippet]]
Breakdown of the XPath Expression
//a: Selects all <a> (anchor) elements in the document.
contains(@ href, 'photo/?fbid'): Filters those elements to find those where the href attribute contains the specified text.
Method 2: Using CSS
Alternatively, if you prefer using CSS selectors, here's how you can achieve the same result:
[[See Video to Reveal this Text or Code Snippet]]
Breakdown of the CSS Selector
a: Selects all <a> (anchor) elements.
[href*='photo/?fbid']: Filters these elements to find those where the href attribute includes the specified substring.
Conclusion
By understanding the differences between CSS and XPath selectors and using the correct syntax, you can efficiently extract HREF links that contain specific text on a webpage using Robot Framework. This knowledge not only helps you troubleshoot potential errors but also enhances your web automation skills.
If you have any further questions or need clarification on any points, feel free to reach out. Happy automating!

Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: