Assistance Needed with XPATH and CSS for Selenium Automation

Can anyone assist me in finding the appropriate XPATH/CSS locator to extract text from the structure below?

<div class="page-header song-wrap">
<div class="art solo-art">
<div class="meta-info">
<h1 class="page-title">
Zehnaseeb

I am looking for a specific locator/XPATH that will return the text "Zehnaseeb" (as shown in this example).

My current method did not produce any results,

driver.findElement(By.xpath(".//*[@id='main']/div/section/div[1]/div[2]/h1")).getText();

Answer №1

Have you attempted pausing for the specific element to appear?

In your code, try utilizing WebDriverWait to wait for visibility before getting the text of the element: String text = new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.page-header h1.page-title"))).getText();

Answer №2

If you happen to be working with C#, I highly recommend using "ScrapySharp" for your HTML parsing needs.

Check out ScrapySharp here!

Document htmlDoc = new HtmlDocument();
htmlDoc.loadHtml(driver.PageSource);
var pageTitle = doc.DocumentNode.CssSelect("h1.page-title").SingleOrDefault().InnerText;

This solution should do the trick.

Answer №3

To ensure the hierarchy is correct, I would recommend checking all elements in between. However, you may also consider simplifying by removing unnecessary elements using the descendant selector //

//*[@id='main']//h1[@class='page-title']

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The HTML element update event was not triggered due to the excessive load of a JavaScript function

I am currently running a JavaScript function that is quite CPU intensive. To provide visual feedback to users when it starts and stops, I am attempting to display a badge on the webpage. Below is the code snippet: function updateView() { console.log(1 ...

The incorporation of SVG within a span disrupts the conventional left-to-right arrangement of its child elements

As I was working with some basic HTML, I encountered an issue. <span> <span>Hello</span> <span>World</span> </span> The above code displays: Hello World However, when I tried adding an SVG element like this: <s ...

Ways to deliberately make a test case fail for a specific data set and then proceed with running the same script in TestNG

Currently, I have a test script that needs to run for "n" amount of data using @DataProvider. The issue arises when I use the "Assert" method to fail the test case for one particular set of data; it halts the execution of the entire Test Script. I am loo ...

Having trouble with the "setValue" property being undefined error. Looking for a solution to input text into a codeMirror element using Selenium WebDriver with Java

Currently, I am attempting to input text into a textarea field that is recognized as CodeMirror. Below is the code snippet I have been working with: {... WebElement scriptField = this.getDriver().findElement(By.cssSelector(".CodeMirror-line>span")); ...

"elementToBeClickable" ensures that the element is both visible and clickable

As a newcomer to Selenium and Java for test writing, I often encounter the issue of needing to click on a web element that is obscured by others. Currently, the elementToBeClickable function only verifies that the element is visible and enabled. Is there ...

Encountering issue: The parseInt method in the Integer type does not accept the parameters (String, Duration)

Looking for some insights, Encountering this error message: The method parseInt(String, int) in the Integer type cannot be used with arguments (String, Duration) Code Implementation: Selenium 4 driver.manage().timeouts().implicitlyWait(Integer.parseInt(C ...

Selenium is struggling to locate any elements that are both visible and clickable

An application I'm working on used to have a JavaScript wait function within the Selenium framework. However, this caused issues with Angular9 and had to be replaced by a better solution. Now, I am facing a challenge where Selenium is unable to find ...

Easy steps for aligning text with shiny html

Looking at the code below, I was able to successfully align the bullet-point text in the popover when hovering over the information circle. However, I am still struggling to justify-align its header right above it, as seen in this image. When I remove the ...

Internet Explorer versions 9 and 10 do not support the CSS property "pointer-events: none"

In Firefox, the CSS property pointer-events: none; functions properly. However, it does not work in Internet Explorer 9-10. Are there any alternative approaches to replicate the functionality of this property in IE? Any suggestions? ...

The component's width appears to be constrained by the use of display flex

I am currently working on a reactjs application that includes a header and another component. Initially, everything looks fine, but when I apply display: flex to their div, it seems to reduce the width of the header. Here are some images showing the issue: ...

Sending Test Identifiers to Sauce Labs using CodeceptJS

For a while now, I've been attempting to get Sauce Labs to provide the names of the tests running with CodeceptJS. Unfortunately, all I could manage so far was to report their success or failure status. To guide me through setting up general reportin ...

Encountered an error in the main thread: org.openqa.selenium.ElementNotInteractableException. The element is not interactable while using Selenium with Java and Edge WebDriver

Testing out some code: import java.time.Duration; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.support.ui.ExpectedConditio ...

Having trouble selecting the button on Shopify using Selenium

Having trouble clicking the "Continue to payment button" on my Shopify site. While I've come across similar posts, most of them focus on js and don't address the spinner aspect of the issue. driver.find_element_by_xpath ('//*[@id="continue_b ...

Responsive Font Sizing in React Native

Does React Native automatically adjust font size based on screen size? If I set the fontSize to 10 for a Text component, will it resize according to different phone sizes? If not, what is the best way to make fonts responsive in React Native? ...

The color used to highlight questions on Stackoverflow

Could someone please help me identify the specific color that stackoverflow uses to highlight the background when a question answer link is followed? For instance, take a look at this link: Is there a float input type in HTML(5)? Appreciate any assistanc ...

Visual feedback: screen flashes upon clicking to add a class with jQuery

I have successfully added a click event to my pricing tables in order to apply an animation class on mobile devices. However, I am facing an issue where every time I click on a pricing option on my iPhone, the screen flashes before the class is applied. Is ...

Is there a way to restrict access to my website to only be opened in the Chrome browser?

Is there a way to prevent my web application from loading when the link is opened in browsers other than Chrome? Can this be achieved using Javascript or Java? I want to restrict the usage of my web application to only Chrome. Any assistance would be appre ...

Error encountered while utilizing the 'Select' function in Selenium with Java due to an 'UnexpectedTagNameException', where the Element was expected to be a "select" but was actually a "div"

I am facing an issue with the drop-down selection in this form. https://i.stack.imgur.com/g3wxg.png In the image above, I am trying to select 'Borrowing Capacity' and I have written the code for it public static void main(String[] args) t ...

Utilize the dimensions of one image to resize others using the JavaScript method .getBoundingClientRect

I am attempting to transfer the width and height measurements of the initial image to all subsequent images on the page. Currently, I have successfully applied the dimensions of the first image to the second one, but I am facing difficulties with the thir ...

Selenium's Firefox driver - A powerful tool for web

Is there a place to download the Firefox driver for Selenium? I have checked this link, but I couldn't find the driver file for download: https://code.google.com/p/selenium/wiki/FirefoxDriver NOTE: I already have Selenium Webdriver IDE for Firefox, ...