selenium can not find html element on firefox - python

I try to verifiy in selenium RC with python:
sel.is_element_present("//div[#id='USER_PHOTOS']/div[1]/span[1]/img)
but looks like can not get it right.
and tried this:
sel.is_element_present("//div[#id='USER_PHOTOS'])
it works.
did anyone why it can't find the sub tag here:
<div class="bx01 deals swirllinks">
<div id="USER_PHOTOS" class="bx01">
<div class="ug_photos">
<span class="u_/LocationPhotos-g294212-d1234659-Fairmont_Beijing-Beijing.html#41195738 fkASDF fkLnk" onclick="setPID(3901); return ta.call('ta.overlays.Factory.photosLB', event, this);">
<img class="ug_photo" alt="coffee machine" src="http://ccm.ddcdn.com/ext/photo-l/02/5d/7c/f6/coffee-machine.jpg"/>
</span>
<span class="u_/LocationPhotos-g294212-d1234659-Fairmont_Beijing-Beijing.html#41195739 fkASDF fkLnk" onclick="setPID(3901); return ta.call('ta.overlays.Factory.photosLB', event, this);">
<img class="ug_photo" alt="bathroom" src="http://ccm.ddcdn.com/ext/photo-l/02/5d/7c/f7/bathroom.jpg"/>
</span>

In your xpath - //div[#id='USER_PHOTOS']/div[1]/span[1]/img , selenium will look for a div under div id = USER_PHOTOS. Your span doesn't come under "USER_PHOTOS" its under the <div class="ug_photos">
You should use //div[#class='ug_photos']//span[1]/img

Related

Python Selenium How to find elements by XPATH with info from TAG and SUB TAG

HTML:
<div id="related">
<a class="123" href="url">
<h3 class="456">
<span id="id00" aria-label="TEXT HERE">
</span>
</h3>
</a>
<a class="123" href="url">
<h3 class="456">
<span id="id00" aria-label="NOT HERE">
</span>
</h3>
</a>
</div>
I'm trying to find & click on <a (inside the div id="related" with class="123" AND where SPAN aria-label contains "TEXT"
items = driver.find_elements(By.XPATH, "//div[#id='related']//a[#class='123'][contains(#href, 'url')]//span[contains(#aria-label, 'TEXT']")
But it's not finding the href, it's only finding the span.
then I want to do:
items[3].click()
How can I do that.
Your XPath has some typo problems.
Try this:
items = driver.find_elements(By.XPATH, "//div[#id='related']//a[#class='123'][contains(#href,'watch?v=')]//span[contains(#aria-label,'TEXT')]")
This will give you the span element inside the presented block.
To locate the a element you should use another XPath.
UPD
To find all the a elements inside div with #id='related' and containing span with specific aria-label attribute can be clearly translated to XPath like this:
items = driver.find_elements(By.XPATH, "//div[#id='related']//a[#class='123' and .//span[contains(#aria-label,'TEXT')]]")

Using beatifulsoup to find text on html

This is my first time using beautifulsoup as a scraper tool and I just follow thru slowly with each step.
I've used soup.find_all("div", class_="product-box__inner") find a list of element I want and this partiful stuff not going thru my mind right now. my question below,
here is the HTML and my target is "$0" and I have tried
element.find("span", title= re.compile("$")) and I can't use element.select("dt > dd > span > span") because there's multiple one with same tag format which I dont need at all, Is there way I can target span data-fees-annual-value="" to get .text working?
<div class="product-box__features-item">
<dt class="f-body-3 product-box__features-label">Annual fee</dt>
<dd class="f-title-5 product-box__features-text u-margin-0">
<span>
<span data-fees-annual-value="">$0</span>
</span>
</dd>
</div>
You are close to your goal with css selectors and they could be used more specific and reference directly on the attribute data-fees-annual-value:
soup.select_one('span[data-fees-annual-value]').text
Example
from bs4 import BeautifulSoup
html="""
<div class="product-box__features-item">
<dt class="f-body-3 product-box__features-label">Annual fee</dt>
<dd class="f-title-5 product-box__features-text u-margin-0">
<span>
<span data-fees-annual-value="">$0</span>
</span>
</dd>
</div>
"""
soup=BeautifulSoup(html,"html.parser")
soup.select_one('span[data-fees-annual-value]').text
Output
$0
If you want to find element by text, use string instead of title:
element.find("span", string=re.compile('$'))
Output:
<span data-fees-annual-value="">$0</span>

How i can click on a div with role button without text? Using Python Selenium

This is the html code exemple:
<div aria-label="Continue" class="my-class" data-visualcompletion="ignore"></div>
<div class="div1-class">
<div class="div1-class2">
<span class="area-span" dir="auto">
<span class="text-span">Continue</span>
</span>
</div>
</div>
<div class="div2-class" data-visualcompletion="ignore"></div>
I'm trying:
continue = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, 'my-class')) )
continue.click()
but it doesn't work in any of the ways I tried.
You should check the xpaths in the browser console.
And try doing this ?
driver.findElement(By.xpath("//div[contains(#class,'my-class')]"));

How to get click on 'href' link using selenium

how do i click on the href link: href="/in/x-y-07976b159/" using selenium?
<a data-control-id="SOR2sXWgS8mDhCUEgFncpQ==" data-control-name="search_srp_result" href="/in/x-y-
07976b159/" id="ember1502" class="search-result__result-link ember-view"> <h3 id="ember1503"
class="actor-name-with-distance search-result__title single-line-truncate ember-view"> <span
class="name-and-icon"><span class="name-and-distance">
<span class="name actor-name">x y</span>
<span data-test-distance-badge="" id="ember1504" class="distance-badge separator ember-view">
<span class="visually-hidden">
1st degree connection
</span>
<span class="dist-value">1st</span>
</span>
</span><!----></span>
</h3>
</a>
You can click it with the help of below command.
driver.find_elements_by_xpath("//a[#id='ember1502']").click();
Where driver is the web driver instance.
You can do it both ways.
1) Find this element (eg. by id) and invoke method .click() on it.
2) Go to page (using driver.get(youcurrenturl + yourextracted href))
Another way of doing using find_element_by_idmethod.
driver = webdriver.Chrome()
driver.get('http://www.YourUrl.com')
driver.find_element_by_id('ember1502').click()

Is there any idea to acess the third a tag link text or click the link with selenium python?

Need to acess the link through selenium but can't
Done:
link1 = driver.find_element_by_xpath("//div[contains(#id,'sidebar1269')]//div[contains(#class,'v1269 hidepiece')]//a[3]")
link1.click()
and also tried:
link2 = driver.find_element_by_id("c0380dac-366a-43db-a2d7-61031e3ac10c")
link2.click()
Source Code:
<div id="sidebar1269" style="wdith:260px; height:600px;overflow-y: auto;" class="normal" bis_skin_checked="1">
<div class="v1269 hidepiece" id="0ba14fb5-f7b3-4667-bcf5-708eda61b09c" bis_skin_checked="1" style="display: block;">
<img src="/Portals/0/1000.png" alt="" width="25">
Candace Novak, DVM</div>
Try below xpath :
//*[contains(text(),'Candace Novak, DVM')]

Categories

Resources