I am trying to get only order numbers like "190795". check the image
What i have tried
self.driver.find_element_by_xpath('//div[#class="sc-kafWEX ihwrOP"]//div/div/div[3]').text
But it will return span text such as "Order number:190795" like this.
I want only "190795"
This is my HTMl code
That is a text node, you can not write an XPath (v1.0) for that, and Selenium make use of XPath v1.0 , so you will have to be dependent on binding language.
Try this:
org_text = self.driver.find_element_by_xpath('//div[#class="sc-kafWEX ihwrOP"]//div/div/div[3]').text
desired_text = org_text.split(':')[1]
print(desired_text)
Related
I'm currently trying to locate this check box. I know I can use a xpath to locate it but I'm trying to see if there's a more efficient way of doing it. The problem I'm seeing is that there are multiple div class with the same name. I'm trying to find this specific one and isolate it. I'm trying to make my code more efficient if possible.
Xpath
/html/body/div/div/div/div[1]/cow-data/cat-panel/section/div[1]/div/div/md- checkbox[4]/div[1]
Element path:
<div class="cd-container" cd-gar-ripple="" cd-gar-ripple-checkbox=""><div class="cd-icon"></div></div>
Code I'm trying to use:
find('cd-container').click
The problem I'm seeing is that the div id 'cd-container' has multiple occurrences on the page and thus this doesn't work. I'm trying to see if I can find a more efficient way of doing this.
As per the HTML cd-container is the value of the class attribute but not id attribute. So your effective line of code will be:
find('.cd-container').click
If you want to find an element (AND THEN), return it's xpath. Use capybara.
This will allow you to locate using text / css selector. And then you can just return the path of the element.
i.e.
page.find('td', text: 'Column 1').path # Random td with text
page.find('#main').path # ID
page.all('div').select { |element| element.text == 'COoL dIv' }.first.path # First div that matches certain text
page.find('.form > div:nth-of-type(2)').path # Specific structured div
page.all('p div li:nth-child(3)').sample.path # Random li
I want to select the text "Auto-Publish" in Span. How can I do it with a CSS selector? with Xpath I know how to do it. I am using Nightwatch for UI automation. I want to make a generic function in the page object which will take a value as a parameter.
SelectFilterValue(value){
.click(`//span[text()="${value}"]`)
}
I can't do it like this since it's an XPath and I have to specify that it's an XPath. If it was a CSS selector I could do it since I don't have to specify that it's a CSS selector. Or is there is any way that I can do it with Xpath too?
CSS does not have any method like text. So in HTMLDOM, it is not possible at this point of time to locate the element based on text.
Moving further, You could do below in nightwatch.js
.useXpath().click('//span[contains(text(), "' + desiredText+ '")]')
and before calling this assign Auto-Publish to the desiredText variable.
Give a specific id to span tag and then edit css. You can also use inline css which will be the best option.
<span style="color:blue;">
I want XPath to return a text with a link embedded in it, can I return both at the same time or am I forced to return:
the link & separately
the text label of the link
You can do some thing like below.
//actor[#color='blue']/concat(#id,",",#class)
I am trying to scrape full reviews from this webpage. (Full reviews - after clicking the 'Read More' button). This I am doing using RSelenium. I am able to select and extract text from the first <p> element, using the code
reviewNodes <- mybrowser$findElements(using = 'xpath', "//p[#id][1]")
which is for less text review.
But not able to extract full text reviews using the code
reviewNodes <- mybrowser$findElements(using = 'xpath', "//p[#id][2]")
or
reviewNodes <- mybrowser$findElements(using = 'xpath', "//p[#itemprop = 'reviewBody']")
It shows blank list elements. I don't know what is wrong. Please help me..
Drop the double slash and try to use the explicit descendant axis:
/descendant::p[#id][2]
(see the note from W3C document on XPath I mentioned in this answer)
As you're dealing with a list, you should first find the list items, e.g. using CSS selector
div.srm
Based on these elements, you can then search on inside the list items, e.g. using CSS selector
p[itemprop='reviewBody']
Of course you can also do it in 1 single expression, but that is not quite as neat imho:
div.srm p[itemprop='reviewBody']
Or in XPath (which I wouldn't recommend):
//div[#class='srm']//p[#itemprop='reviewBody']
If neither of these work for you, then the problem must be somewhere else.
I am working on project with java and selenium webdriver 2.44, we need the closest element of text. For example I have email text on page and i need the closest element that is text box. I have run the below given xpath query on facebook.com. However, it finds numbers of input type that is email and text. We do not need jQuery to run as we are using selenium webdriver.
.//*[contains(text(),'Email')]/following::input[#type='email' or #type='text']
Can anyone provide me xpath query to find closest element on page. Or a tutorial site where I can learn how to query.
Not a perfect solution, though in facebook scenario u can find first element as:
(.//*[contains(text(),'Email')]/following::input[#type='email' or #type='text'])[1]
Another thing to add if you are getting multiple elements for your query you can use:
List<WebElement> txtList = new ArrayList<WebElement>();
txtList = driver.findElements(By.xpath(".//*[contains(text(),'Email')]/following::input[#type='email' or #type='text']"));
and use txtList.get(0).sendKeys("abcd");