Below I describe the issue I need help with.
Description
I collect stock data from https://www.di.se/bors/large-cap/, and to get all necessary data I need to click on the tab “nyckeltal”.
Problem
The problem is when I click on the tab “nyckeltal” the element is not found so I get an exception and I am not directed to “nyckeltal”. This worked before but changes has been made on the site i.e classname has been changed.
I do as following to click on the tab “nyckeltal” with Selenium and Java with xpath:
driver.findElement(By.xpath(//a[contains(@class,'instrument-table-tab-menu'') and contains(@data-target,'2')])).click();
Question
How to click on the tab “nyckeltal” with Selenium? As told above this has been working before!
This is a website very useful when you’re working with Selenium:
- https://bugbug.io/xpath-selector-builder/
Once you understand how it works, it’s gonna help you a lot.
Now, about your problem, as your code shows, you’re looking for an HTML element with the class instrument-table-tab-menu and @data-target, ‘2’.
As I see, if the page use to change, you better look for the text directly of the button and can add some other params, such as Class Name, but, both of the should use the contains flag.
Here an example:
String xpath = "//button[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'nyckeltal')]";
The translate option helps to ignore capitalize letters, in this case, Selenium is going to look for a button element with the text “nyckeltal”
Now, if you want to ensure the option, can add aditional info to the Xpath, such as the data-target attr.
String xpath = "//button[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'nyckeltal') and @data-target='table_2']";
Luis Villalba Ballesteros is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1