How to find below element ? . I need to click the Excel image - python

Code tried:
driver.find_element(By.CSS_SELECTOR, ".exportPointer").click();
driver.find_element(By.CSS_SELECTOR,"//img[#src='/xyz/img/excel_export.png']").click();
driver.find_element(By.CSS_SELECTOR,"./xyz/img/excel_export.png").click()
Html
<span onmousedown="UX.preventSelectionUntilMouseUp();" onclick="abcd_.vars.tables[0].toCsv([true]);">
<img src="/xyz/img/excel_export.png" title="Export to Excel" class="exportPointer">
</span>

If you are using By.CSS_SELECTOR you need to use it as a tuple
driver.find_element((By.CSS_SELECTOR, '.exportPointer'));
The recommended way though is to use find_element_by_*
driver.find_element_by_css_selector('.exportPointer')
Or by class
driver.find_element_by_class_name('exportPointer')
*The second and third attempts are not valid css_selector, you used xpath syntax.

Related

Can't find element using by.XPATH

I'm trying to find one element using Selenium in Python using the following code:
element_A = driver.find_element(By.XPATH, '/html/body/div[5]/div[1]/div[2]/div[3]/div/div/div/div[1]/div[1]/div[2]/div[1]/div[3]/div[1]/i')
in this HTML code:
<div class="display-flex>
<span class=" cc-customer-info__value"="">212121 <i title="copiar" class="fa fa-clone _clipboard_" data-clipboard="212121 "></i> <br>
</div>
I want the number 212121, but I'm getting no such element error. The problem is this number can be different every time that I open the website. It's the number of the customer.
Is it possible to help me to locate this element?
I'm also trying to find two more elements:
Customer profile, it's also change
<text class="legend-graph font-menu" x="416" y="38">Customer Profile: A</text>
and Diamond, that also can vary
<span class="cc-customer-info__value ">Diamond</span>
Thank you!
For the first one, the span element has the text as number, not the tag.
driver.find_element(By.XPATH, '/html/body/div[5]/div[1]/div[2]/div[3]/div/div/div/div[1]/div[1]/div[2]/div[1]/div[3]/div[1]/span)
try this it may work, or try to shorten the xpath you are creating. this is absolute xpath.
try and use selenium ide to record the scenario and check what locator is being captured for the element.
you could also try devtools/ and other xpath locating tools which are available as chrome extentions.

Using aria-label to locate and click an element with Python3 and Selenium

I want to click or more respectively, expand the "Any time" button. I've tried to locate the element by class_name and xpath. The problem is that the class and xpath are the same for all three 'options'. So, I would like to select and click or expand on that element by using the aria-label. I found a couple of suggestions, but it didn't work for me. Most importantly, I try to do that in python 3. I also tried:
driver.find_element_by_xpath(""" //div*[#aria-label='Any Time'] """).click()
but it doesn't work.
Could anyone please help me? Many thanks in advance!
<div class="hdtb-mn-hd" aria-haspopup="true" role="button" tabindex="0" aria-label="Any country"><div class="mn-hd-txt">Any country</div><span class="mn-dwn-arw"></span></div>
<div class="hdtb-mn-hd" aria-haspopup="true" role="button" tabindex="0" aria-label="Any time"><div class="mn-hd-txt">Any time</div><span class="mn-dwn-arw"></span></div>
<div class="hdtb-mn-hd" aria-haspopup="true" role="button" tabindex="0" aria-label="All results"><div class="mn-hd-txt">All results</div><span class="mn-dwn-arw"></span></div>
So, I was just wrestling with this for the last couple days, and it was proving to be a huge headache. The aria-label was basically the only reliable attribute, and the xpath solution was not working for me.
On a whim, I tried using:
driver.find_elements_by_css_selector("[aria-label=XXXX]")
where XXXX was the aria labels that I was searching for in single quotes (e.g. "[aria-label='More']"). Worked like a charm.
All this to say, try using the css selector. It just works.
Using the aria-label property you can try the following xpath:
driver.find_element_by_xpath("//div[#aria-label='Any time']/div[#class='mn-hd-txt' and text()='Any time']");
OR
driver.find_element_by_xpath("//div[#aria-label='Any time']/div[#class='mn-hd-txt'][text()='Any time']");
If using aria-label property is not a mandatory requirement you can use the following:
driver.find_element_by_xpath("//div[#class='hdtb-mn-hd']/div[#class='mn-hd-txt' and text()='Any time']");
OR
driver.find_element_by_xpath("//div[#class='hdtb-mn-hd']/div[#class='mn-hd-txt'][text()='Any time']");

Selenium-Python: Class containing link-text

I am using Python & Selenium to scrap the content of a certain webpage. Currently, I have the following problem: There are multiple div-classes with the same name, but each div-class has different content. I only need the information for one particular div-class. In the following example, I would need the information in the first "show_result"-class since there is the "Important-Element" within the link text:
<div class="show_result">
<a href="?submitaction=showMoreid=77" title="Go-here">
<span class="new">Important-Element</span></a>
Other text, links, etc within the class...
</div>
<div class="show_result">
<a href="?submitaction=showMoreid=78" title="Go-here">
<span class="new">Not-Important-Element</span></a>
Other text, links, etc within the class...
</div>
<div class="show_result">
<a href="?submitaction=showMoreid=79" title="Go-here">
<span class="new">Not-Important-Element</span></a>
Other text, links, etc within the class...
</div>
With the following code I can get the "Important-Element" and its link:
driver.find_element_by_partial_link_text('Important-Element'). However, I also need the other information within the same div-class "show-result". How can I refer to the entire div-class that contains the Important-Element in the link text? driver.find_elements_by_class_name('show_result') does not work since I do not know in which of the div-classes the Important-Element is located.
Thanks,
Finn
Edit / Update: Ups, I found the solution on my own using xpath:
driver.find_element_by_xpath("//div[contains(#class, 'show_result') and contains(., 'Important-Element')]")
I know you've found an answer but I believe it's wrong since you would also select the other nodes because Important-Element is still in Non-Important-Element.
Maybe it works for your specific case since that's not really the text you're after. But here are a few more answers:
//div[#class='show_result' and starts-with(.,'Important-Element')]
//div[span[text()='Important-Element']]
//div[contains(span/text(),'Important-Element') and not(contains(span/text(),'Non'))]
There are more ways to write this...
Ups, i found the solution on my own via xpath:
driver.find_element_by_xpath("//div[contains(#class, 'show_result') and contains(., 'Important-Element')]")

how to use selenium locate inpurt element after a specific text with python

for some reason
I need to input some text after the 'Test'
but the input name will always auto change with different name and even some different format , so I can't locate it via a fixed CSS or Xpath
<tr class="123"><td class="456">
<div class="789">Test</div></td><td><input size="16" maxlength="15" name="ABC"></td>
i try to locate via xpath like this
//*[descendant::*[contains(text(),'Test')]]/input[1]
but fail .. I thought I should do in wrong way , help....
An XPath to locate the input following the text "Test":
"//div[.='Test']/following::input[1]"
You can use following-sibling axis like this :
//td[div='Test']/following-sibling::td[1]/input

Selenium XPath multiple attributes including text

Here is the HTML I'm dealing with
<a class="_54nc" href="#" role="menuitem">
<span>
<span class="_54nh">Other...</span>
</span>
</a>
I can't seem to get my XPath structured correctly to find this element with the link. There are other elements on the page with the same attributes as <a class="_54nc"> so I thought I would start with the child and then go up to the parent.
I've tried a number of variations, but I would think something like this:
crawler.get_element_by_xpath('//span[#class="_54nh"][contains(text(), "Other")]/../..')
None of the things I've tried seem to be working. Any ideas would be much appreciated.
Or, more cleaner is //*[.='Other...']/../.. and with . you are directly pointing to the parent element
In other scenario, if you want to find a tag then use css [role='menuitem'] which is a better option if role attribute is unique
how about trying this
crawler.get_element_by_xpath('//a[#class="_54nc"][./span/span[contains(text(), "other")]]')
Try this:
crawler.get_element_by_xpath('//a[#class='_54nc']//span[.='Other...']');
This will search for the element 'a' with class as "_54nc" and containing exact text/innerHTML "Other...". Furthermore, you can just edit the text "Other..." with other texts to find the respective element(s)

Categories

Resources