This is the xml snippet [div id=":6r" class="Ar Au" style="display: block;"]
To locate an element by css selector, i tried by giving the div element followed by the class name with dot(.), but it did not work
driver.find_element_by_css_selector("div.Ar.Au").send_keys("ABC")
However the below worked,
driver.find_element_by_css_selector(".Ar.Au div).send_keys("ABC")
Can someone please tell me why the 1st one did not work ?
Below is the error log using the 1st option:
selenium.common.exceptions.ElementNotInteractableException: Message:
element not interactable
div.Ar.Au mean locate the <div> which has these class name (ArandAu) as an attribute. So in this case its gonna locate <div id=":6r" class="Ar Au" style="display: block;">
.Ar.Au div this mean locate the child <div> tag under a tag which has class name (Arand Au). So this one is locating the child <div> under <div id=":6r" class="Ar Au" style="display: block;">
Note: dot(.) is used in CSS selector to represent a class name and select compound classes
Related
How can I get the text "950" from the div that has neither a ID nor a Class with python selenium?
<div class="player-hover-box" style="display: none;">
<div class="ps-price-hover">
<div><img class="price-platform-img-hover"></div>
<div>950</div>
</div>
I dont know how I could access this div and its text.
In case player-hover-box is an unique class name you can use the following command
price = driver.find_element_by_xpath('//div[#class="player-hover-box"]/div/div[2]').text
In case there are more products on that page with the similar HTML structure your XPath locator should contain some unique relation to some other element.
I was trying to scrape a website, and I need to select only the ul element inside the div with a class "Slider__SliderWrapper-sc-143uniy-0 jrPmnS", however, since there are many div tags with the same class, the only way I have to select just the ul I need is by looking at the href of the a tag, the one inside the h2.
I can't use xpath, because div tags always change position.
<div>
<h2><a class="slider-components__SectionLink-sc-1r2bduf-3 jchpWs" href="rightOne">Right!</a></h2>
<div class="Slider__SliderWrapper-sc-143uniy-0 jrPmnS">
<ul class="Slider__List-sc-143uniy-1 MTYOL">
the right ul
</ul>
</div>
</div>
<div>
<h2><a class="slider-components__SectionLink-sc-1r2bduf-3 jchpWs" href="wrongOne">Something else</a></h2>
<div class="Slider__SliderWrapper-sc-143uniy-0 jrPmnS">
<ul class="Slider__List-sc-143uniy-1 MTYOL">
the wrong ul
</ul>
</div>
</div>
I thought about using css selector but I don't know how to, any help?
You definitely CAN use xpath to access the href attribute AND it's contents:
//a[contains(#href,'rightOne')]
and for the ul:
//h2/a[contains(#href,'rightOne')]/../following-sibling::div/ul
try xpath
//a[#href='rightOne']/../following-sibling::div/ul
Explanation :
You cannot use css_selector or any other locator since you are depending on a tag and you have to traverse upwards in DOM first, we are using /.. for that, alternatively you can use /parent::h2 and the next following-sibling using /following-sibling::div and then finally ul child
You cannot get a parent element with css selector, as it's not possible. Check here Is there a CSS parent selector?
In your case you would need to get the parent of a[href=rightOne] and get the ul of the following sibling.
With css you could use one of these locators:
div:nth-child(1) .Slider__SliderWrapper-sc-143uniy-0.jrPmnS>.Slider__List-sc-143uniy-1.MTYOL
Or
div:nth-child(1) .Slider__SliderWrapper-sc-143uniy-0.jrPmnS>ul
I would select any of XPaths proposed in other two answers if there are not restrictions on selectors.
But, if you are using such libraries as BeautfulSoup, you will have to use css selectors, as it does not support XPath. So, use the ones I proposed.
I have the below HTML snippet.
<div class="header">Planets</div>
<div class="event">Jupiter</div>
<div class="event">Mars</div>
<div class="header">Stars</div>
<div class="event">Acturus</div>
<div class="event">Pleaides</div>
Using driver.find_elements_by_class_name("event"), I am able to retrieve all the div tags with class "event".
I would want to navigate to the previous sibling and retrieve the div tag with class "header" for each WebElement.
Switch to by find_elements_by_xpath
driver.find_elements_by_xpath("//div[#class='event']/preceding-sibling::div[#class='header']")
I am trying to use Python Selenium Firefox Webdriver to grab the h2 content 'My Data Title' from this HTML
<div class="box">
<ul class="navigation">
<li class="live">
<span>
Section Details
</span>
</li>
</ul>
</div>
<div class="box">
<h2>
My Data Title
</h2>
</div>
<div class="box">
<ul class="navigation">
<li class="live">
<span>
Another Section
</span>
</li>
</ul>
</div>
<div class="box">
<h2>
Another Title
</h2>
</div>
Each div has a class of box so I can't easily identify the one I want. Is there a way to tell Selenium to grab the h2 in the box class that comes after the one that has the span called 'Section Details'?
If you want grab the h2 in the box class that comes after the one that has the span with text Section Details try below xpath using preceding :-
(//h2[preceding::span[normalize-space(text()) = 'Section Details']])[1]
or using following :
(//span[normalize-space(text()) = 'Section Details']/following::h2)[1]
and for Another Section just change the span text in xpath as:-
(//h2[preceding::span[normalize-space(text()) = 'Another Section']])[1]
or
(//span[normalize-space(text()) = 'Another Section']/following::h2)[1]
Here is an XPath to select the title following the text "Section Details":
//div[#class='box'][normalize-space(.)='Section Details']/following::h2
yeah, you need to do some complicated xpath searching:
referenceElementList = driver.find_elements_by_xpath("//span")
for eachElement in referenceElementList:
if eachElement.get_attribute("innerHTML") == 'Section Details':
elementYouWant = eachElement.find_element_by_xpath("../../../following-sibling::div/h2")
elementYouWant.get_attribute("innerHTML") should give you "My Data Title"
My code reads:
find all span elements regardless of where they are in HTML and store them in a list called referenceElementList;
iterate all span elements in referenceElementList one by one, looking for a span whose innerHTML attribute is 'Section Details'.
if there is a match, we have found the span, and we navigate backwards three levels to locate the enclosing div[#class='box'], and find this div element next sibling, which is the second div element,
Lastly, we locate the h2 element from its parent.
Can you please tell me if my code works? I might have gone wrong somewhere navigating backwards.
There is potential difficulty you may encounter, the innerHTML attribute may contain tab, new line and space characters, in that case, you need regex to do some filtering first.
I'm trying to find all elements on a page that have not already been clicked, or 'favorited.'
Here is the html for an element that is favorited:
<a class="button-fave unfavorited-button favorited-button" rel="78853399" alt="Add to favorites">
<div class="button-spinner"></div>
<span class="status-text">Favorite</span>
</a>
Here is the html code for when the element is not favorited:
<a class="button-fave unfavorited-button" rel="78853399" alt="Add to favorites">
<div class="button-spinner"></div>
<span class="status-text">Favorite</span>
</a>
I have tried:
driver.find_element_by_class_name('button-fave unfavorited-button')
but I get the following:
The given selector button-fave unfavorited-button is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: Compound class names not permitted
The following works, but it doesn't discern between favorited elements and unfavorited elements:
driver.find_element_by_class_name('button-fave')
You can find all a elements not having favorited-button class:
driver.find_elements_by_css_selector("a:not(.favorited-button)")