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.
Related
I need to extract the experience section and I need to scrape companies where he has worked(either previous or current) with python, I have tried but it is not working.
my code -
div = driver.find_element('div', class_='t-14 t-normal')
span = div.find_element('span', {'aria-hidden': 'true'})
text = span.get_text()
print(text)
Inspect element looks like -
this is just for a single company, btw all the inspect looks same for rest of the companies I checked.
<span class="t-14 t-normal">
<span aria-hidden="true">
<!---->
"Financial Times . Full-time"
<!---->
</span>
link - https://www.linkedin.com/in/fred-thompson-8a892a19b/
So I just want to extract all the companies which are there in the inspect element. I am unable to do, it seems tough to apply a logic which will loop through all companies and get the names.
You are mixing Selenium syntax with BeautifulSoup syntax.
If you want to do this with Selenium your syntax should be as following:
elements = driver.find_elements(By.CSS_SELECTOR, ".t-14.t-normal span[aria-hidden='true']")
for element in elements:
print(element.text)
However this will not work perfect since locators you are using here will match irrelevant elements too.
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.
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']");
I've written an xpath to get to a certain element but unfortunately it is not being able to.
I can't understand where i'm getting deviated. Hope somebody will provide me with a little help.
I'm trying to locate the address from the below elements.
Here are the elements:
<div class="detail-contact-address">
<i class="fa fa-map-o"></i>
Address:36 Sukhumvit, Soi 15, Wattana, Klongtoey-nua, Bangkok 10110, Thailand
</div>
I was trying with:
//div[#class='detail-contact-address']/*[contains(#class,'fa-map-o')]/../text()
User below xpath to get the text :
//div[#class='detail-contact-address']
OR based on some string match
//div[#class='detail-contact-address'][contains(.,'Address')]
So you will get the text like
print driver.find_element_by_xpath("//div[#class='detail-contact-address']").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')]")