I'm trying to figure out the answer to a problem I've recently discovered. I have some HTML as indicated below and what I want to do, is send an HTML snipped to selenium so it can click on the checkbox. However, I don't want to lookup by id because as you can see, the id is ugly. Instead, I am looking for an alternative method.
HTML:
<li class= "zone odd open night">...</li>
<li class= "zone even open night">...</li>
<li class= "zone odd open night">...</li>
<label for="srr-2-1397538000">Room 226 1:00 AM</label>
<input type="checkbox" name="srr-2-1397538000" id="srr-2-1397538000" value="Y" class="interactive">
<span class-"drag-handle">...</span>
</li>
<li class="zone even open night">...</li>
As you can see, I'm trying to lookup that specific checkbox to click but I don't want to do it by looking up via id. I would rather look up by "Room" or something more generic.
Does anyone have any suggestions as to how to do this? With using selenium, or some other webdriver?
Also, I've tried the classic:
element = driver.find_element_by_id("srr-2-1397538000")
element.click()
But as I said, this is not what I want.
Thank You
Can you use the tag name?
element = driver.find_element_by_tag_name('input')
element.click()
If you want to look it up by Room, you can use xpath:
element = driver.find_element_by_xpath("//label[contains(text(),'Room')]/following-sibling::input")
element.click()
Related
I have a bellow HTML:
<div class="row">
<div><span class="checkbox"></span></div>
<a title="something"></div>
</div>
<div class="row">
<div><span class="checkbox"></span></div>
<a title="somethingelse"></div>
</div>
I would like to click on the checkbox if the a's title is something. I do not know how many rows are there so I need to check every row and if there the title == 'something' click the previous checkbox.
I tried to implement this in Python and Selenium, but so far could not succeed. I can check for a like:
driver.find_element_by_xpath(f"//div[contains(#class, 'row')]//a[#title='something']")
But how I can select the previous element?
Does anybody have a recommendation?
I am using get_attribute and trying to extract title and then putting a if clause that if title attribute text matches with your expected string then click on the above check box.
try this :
actual_text = driver.find_element_by_xpath(f"//div[contains(#class, 'row')]//a[#title='something']")
if actual_text.get_attribute('title') == 'your expected string here':
driver.find_element(By.XPATH, "//div[contains(#class, 'row')]//a[#title='something']/../child::span")
Try this:
driver.find_element_by_xpath("//div[#class='row']/a[#title='something']/..//span[#class='checkbox']").click()
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()
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()
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.
I'm trying to have click a button in the browser with Selenium and Python.
The button is within the following
<div id="generate">
<i class="fa fa-bolt"></i>
<span>Download Slides</span>
<div class="clear"></div>
</div>
Chrome's dev console tells me the button is within <span> but I have no idea how to reference the button for a .click().
Well, if you just want to click on an element without an id or name, I'd suggest three ways to do it:
use xpath:
driver.find_element_by_xpath('//*[#id="generate"]/span')
use CSS selector:
driver.find_element_by_css_selector('#generate > span')
Just try .find_element_by_tag_name() like:
driver.find_element_by_id('generate').find_elements_by_tag_name('span')[0]
Note that this way first try to get the generate <div> element by it's id, and then finds all the <span> elements under that <div>.
Finally, gets the first <span> element use [0].