Selenium finding link by text when text-transform present - python

I have this HTML
<div class="tabsAction">
<a class="butAction" href="" title="" data-original-title="">Validar</a>
<a class="butAction" href="" title="" data-original-title="">Copiar</a>
<a class="butAction" href="" title="" data-original-title="">Convertir en plantilla</a>
<a class="butActionDelete" href="" title="" data-original-title="">Eliminar</a>
</div>
I'm trying to select the Validar link.
driver.find_element(By.LINK_TEXT, "Validar")
but selenium can't find it.
However if I do this:
for link in links:
print(link.text)
I get this:
VALIDAR
COPIAR
CONVERTIR EN PLANTILLA
ELIMINAR
I've checked and the class .butAction has a text-transform: uppercase; css.
I swear this 100% used to work just yesterday, why is it not working now? What am I missing?

Instead of By.LINK_TEXT it's better to use By.XPATH.
In this case you only need to know what text appears in the web element on the page, not how it presented to user.
This should work:
driver.find_element(By.XPATH, "//a[#class='butAction'][text()='Validar']")

I rarely use By.LINK_TEXT but I do remember a few times where it acts... odd in cases where the text is lowercase in the HTML but upper case on the page. Did you ever try all caps in your locator?
driver.find_element(By.LINK_TEXT, "VALIDAR")

Related

Having trouble finding/clicking specific images within a div class through selenium

Practicing web scraping through selenium by opening user's dating profiles through a dating site. I need selenium to save a href link for every profile on the page but not sure how to go about selecting it since each link is different for every profile and image. All of the profiles start with the same two div class/style which is "member-thumbnail" and "position: absolute". Thank you for any help that you can offer.
<div class="member-thumbnail">
<div style="position: absolute;">
<a href="/Member/Details/LvL-Up">
<img src="//storage.com/imgcdn/m/t/502b24cb-3f75-49a1-a61a-ae80e18d86a0" class="presenceLine online">
</a>
</div>
</div>
Try using more general selector as follow
.member-thumbnail a
photo = browser.find_element_by_css_selector('.SELECTOR #GOES HERE').click()
So it should look something like that
photo = browser.find_element_by_css_selector('.member-thumbnail a').click()

Click ComboButton item with Selenium

I am trying to preform a simple click, but cannot find out what way to find it due to the type of element it is.
<div class="active">
<div class="action-title">Reconcile All</div>
<div class="action-description">Reconcile all IPv4 addresses</div>
</div>
<div class="active">
<img src="/images/icons/small/checks.gif" border="0">
</div>
I have tried doing it several ways. Such as,
driver.find_elements_by_link_text("Reconcile All").click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Reconcile All"))).click()
I even tried based of the icon
driver.find_element_by_xpath("//*[contains(#src,'/images/icons/small/checks.gif')]").click()
Thanks in advance for any help
Div element can't click using link_text try Use following xpath and Webdriverwait to click.
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//div[#class='active']//div[#class='action-title'][contains(.,'Reconcile All')]"))).click()

selenium python find link by href text and click it

I have my script to login to a site. i then need to click on another link which is contained in an
<a href> </a>
I have tried multiple methods without success. The link I need "Available Deployments" only appears after clicking a dropdown box called "Job Board".
The site code looks like this:
<li class="">
<a aria-expanded="false" class="dropdown-toggle" data-toggle="dropdown" href="portalPost?s=a1W390000045MxAEAU&p=a1V39000003y7e1EAA" role="button">Job Board <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="portalPage?s=a1W390000045MxAEAU&p=a1V39000003y7dbEAA">Available Deployments
</a>
</li>
i've tried a couple of versions, without success:
-SNIP-
driver.find_element_by_name("logmein").click()
driver.find_element_by_linkText("Job Board").click()
driver.find_element_by_linkText("Available Deployments").click()
and
-SNIP-
driver.find_element_by_name("logmein").click()
driver.find_element_by_xpath(u'//a[text()="Job Board"]').click()
driver.find_element_by_xpath(u'//a[text()="Available Deployments"]').click()
The errors I get typically look like:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[text()="Available Deployments"]"}
It looks like there is a whitespace in text of the element.
You have to use normalize-space to trim text. See an example below.
driver.find_element_by_xpath(u'//a[text()="Job Board"]').click()
waitForPresence = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.XPATH,'//a[normalize-space(text())="Available Deployments"]')))
driver.find_element_by_xpath('//a[normalize-space(text())="Available Deployments"]').click()
I solved this with the following:
driver.find_element_by_link_text("Job Board").click()
driver.find_element_by_link_text("Available Deployments").click()

Navigating website using selenium

I am trying to scrape information from a website but am having trouble navigating it using Selenium. The site uses ng-click to update a table so I must activate different tabs on the page to get the information I want. This is the html that generates the tabs:
<ul class="tabs swiper-wrapper" ng-class="{'swiper-wrapper' : swiperActive }">
<li ng-repeat="category in Report.Winners track by $index" ng-click="updateCategory(category.key)" ng-class="{'active' : category.key == activeCategory, 'swiper-slide' : swiperActive }" class="ng-scope active">
<p class="category text-small ng-binding">Category 1</p>
<p class="winner">
</p>
</li><li ng-repeat="category in Report.Winners track by $index" ng-click="updateCategory(category.key)" ng-class="{'active' : category.key == activeCategory, 'swiper-slide' : swiperActive }" class="ng-scope">
<p class="category text-small ng-binding">Category 2</p>
<p class="winner">
</p>
</li><li ng-repeat="category in Report.Winners track by $index" ng-click="updateCategory(category.key)" ng-class="{'active' : category.key == activeCategory, 'swiper-slide' : swiperActive }" class="ng-scope">
<p class="category text-small ng-binding">Category 3</p>
<p class="winner">
</p>
</li>
</ul>
I have figured out how to scrape the information from "Category 1" since it loads by default. How do I navigate to "Category 2" and "Category 3" so I can scrape those as well? Thanks!
Update:
I ended up using this to find the links for each category:
available_categories_links = browser.find_elements_by_css_selector("ul > [ng-click*=updateCategory]")
And then I loop through them like this:
for x in range(len(available_categories_links)):
available_categories_links[x].click()
Doing it this way doesn't let me access different tabs by name like I had originally hoped to do, and it's probably not the most efficient or very robust, but it gets the job done in my particular case.
I assume your tabs load on runtime.
So, to activate the tab, you need to use FindElement(By) to locate the tab and click it before you could access the information from that tab.
According to your code, no Id found to use FindElement(By.Id). So I suggest you use either FindElement(By.CSSSelector) or FindElement(By.Xpath) which you could copy the Locator string from browser's development tool, i.e. in Google Chrome right click==> inspect==>Copy==>Copy Selector or Copy Xpath.
After you got the Tab Element, i.e.
IWebElement tab = driver.FindElement(By.CSSSelector);
tab.Click();//Tab activated
//...Do your thing afterwards.
Hope this helps.

Can't parse HTML by the Python

I use the HTMLParser library for parsing html code. When I override handle_starttag method i retrive only fragments with "root" elements. I need extract some links from the table. I split the html string and try parse fragments, but the result the same. Whent i try parse the following fragment:
<td class="lineItemMainInfo" width="100%">
<div class="lineItemGroup">
<div><span class="small productTitle"><strong>
<a rel="nofollow" href="/dp/B007R5YFS4/ref=wl_fv/191-7812654-8275300?_encoding=UTF8&colid=1VII2NY76H4UZ&coliid=I17H6RZSYMY3L1">
Amazon Kindle Paperwhite Leather Cover, Onyx Black (does not fit Kindle or Kindle Touch)
</a>
</strong></span></div>
<div class=lineItemPart style="margin-top: 40px;"><span class=wlPriceBold>$39.99</span></div>
<div style="margin-top: 40px;"><span class="swSprite s_add2CartSm " border="0" vspace="0" hspace="0"style="vertical-align:middle;margin-bottom:2px;" alt="Add to Cart"></span></div>
</div>
</td>
I recieve only td tag without any nested tags.
Anybody had this problem?
Can I humbly suggest a replacement for HTMLParser - BeautifulSoup? I am sure it will help you to solve your problem very fast.

Categories