I have 2 divs in code under 1 li. I need to select second one:
<li>
<div id='1'>Stable Text</div>
<div>Unstable Text</div>
</li>
I can find only first one using text's name, as its stable. But I need second one.
using xpath with //li/div[2] will not work because place of this data is not stable.
You can use following-sibling.
//div[text() = 'Stable Text']/following-sibling::div
You can have a look here for more information.
you can try using a cssSelector as well,
li > div#1 + div
Here '+' is used to locate following sibling..you can refer this for more info on selectors.
Related
I have the following HTML code, and I need to have an XPath expression, which finds the table element.
<div>
<div>Dezember</div>
<div>
<div class="dash-table-container">more divs</div>
</div>
</div>
My current Xpath expression:
//div[./div[1]/text() = "Dezember"]/preceding::div[./div[2][#class=dash-table-container]
I don't know how to check if the dash table container is the last one loaded, since I have many of them. So I need the check if it's under the div with "Dezember" as a text because the div's before with the other months are being loaded faster.
I want the XPATH to select the "dash table container" div.
Thanks in advance
To select the div with the text content of "more divs", you can use
//div/div[#class="dash-table-container" and ../preceding-sibling::div[1]="Dezember"]
and to select its parent div element, use
//div[div/#class="dash-table-container"][preceding-sibling::div[1]="Dezember"]/..
I figured it out.
//div[preceding-sibling::div="Dezember"]/div[#class="dash-table-container"]
worked perfectly for me.
I'm using Selenium to scrape a Web Page and I'm having some problems targeting some attributes.
The page I'm trying to scrape looks like this:
<div>
<span abc> content </span>
<span def> content2 </span>
<div>
My goal would be to retrieve the text within the "span abc" tag, without selecting the other text included in the "span def" tag.
I've tried multiple approaches and looked at a lot of different resources but I wasn't able to find the right approach, since I don't want to select all the spans at the same time and I don't want to search based on the text within the tags.
A simple approach would be indexing cause you do not want to select based on
since I don't want to select all the spans at the same time and I
don't want to search based on the text within the tags.
If abc is an attribute please use :
//div/span[#abc]
or
with indexing :
(//div/span[#abc])[1]
If you only want to pull the first span out of these two, you could easily do this with the XPATH. It would look like this:
span = driver.find_element_by_xpath("/html/body/div/span[1]").text
if you want to pull every span, but execute commands with each of these you could do:
span = len(driver.find_elements_by_xpath("/html/body/div/span"))
m = 1
while m <= 0:
span = driver.find_element_by_xpath("/html/body/div/span["+str(m)+"]")
print(span.text)
m = m + 1
You can use xpath like //span[1]/text() for get text inside of the <span> tag
span = driver.find_element_by_xpath("/html/body/div/span[1]/text()")
<div class='into'>
<div class="state " rel="AA" style="width:80px;">AA (1028)</div>
<div class="state " rel="BB" style="width:80px;">BB (307)</div>
</div>
I'd like to select one of elements rel="AA" or rel="BB" to click on it, tried several ways. The most usable idea was:
browser.find_element_by_xpath("//div[#class='into']/[text()='AA']").click()
However there is a number after the text what is various.
browser.find_element_by_xpath("//div[#class='into']/[rel='AA']").click()
And this not works.
Use the following xpath
browser.find_element_by_xpath(".//div[#class='into']/div[#rel='CA']").click()
Also can use normalize-spacemethod to omit the spaces in your class name like below -
browser.find_element_by_xpath(".//div[normalize-space(#class)='state'][#rel='AA']").click()
If you need your XPath to match one of elements with attributes rel="AA" or rel="BB" (in case one of them might not be present on page) then try below:
browser.find_element_by_xpath("//div[#class='into']/div[#rel="AA" or #rel="BB"]").click()
If you want to use your example with text() then you could use either of the following:
browser.find_element_by_xpath("//div[#class='into']/div[contains(text(), 'AA')]").click()
or
browser.find_element_by_xpath("//div[#class='into']/div[starts-with(text(), 'AA')]").click()
otherwise use the answer given by #lauda and use #rel to declare it as an attribute
How can I select the second <a> tag from the following snippet?
<div class="hovno">
<a href='...'></a>
<a href='...'></a>
</div>
I know that I can find the first <a> tag by using:
driver.find_element_by_css_selector("div.hovno a")
But I don't know how to select the second <a> tag.
You can always find all direct a children and get the second element:
driver.find_elements_by_css_selector("div.hovno > a")[1]
Or, according to the example, the last element would work too:
driver.find_elements_by_css_selector("div.hovno > a")[-1]
nth-of-type pseudo-class is also an option:
driver.find_element_by_css_selector("div.hovno > a:nth-of-type(2)")
You should use nth-of-type
driver.FindElement(By.CssSelector("div.hovno a:nth-of-type(2)");
Im no sure but try this
driver.find_element_by_css_selector("div.hovno").find_element_by_tag_name('a')[2]
The HTML looks like this:
<span class="MenuIcons searchButton"></span>
... (some stuff)
<a data-bind="" url="/ParagonLS/Search/Property.mvc/Index/1" tabdescription="RESIDENTIAL" subtabdescription="Criteria" subtabmaxallowed="3" targetex="" rel="" class=" SearchByClass1 " subtabgroup="true" subtabgroupadd="true" subtabstartindex="0" fullwindow="False" hideaddressbar="False">TEXT</a>
I can get to the span using:
driver.find_element_by_css_selector(".MenuIcons.searchButton")
But since the span is a drop down menu I need to get to the inner element, but don't know how since it has spaces around its class name. What do I do?
import time
driver.find_element_by_css_selector(".MenuIcons.searchButton").click()
time.sleep(1)
driver.find_element_by_partial_link_text("TEXT").click()
You can do this and click the link.
I suggest you to use xpath instead since the class contains space.
//a[contains(#class,'SearchByClass1')]
Text based search is also another possibility.
//a[.='TEXT']
Edit
Executing javascript since the element is hidden as per OP's comment
test = driver.execute_script("return document.querySelector(\"a[class*='SearchByClass1']\").innerHTML;");
print(test)
print
TEXT