splinter click button with no attributes - python

I'm using splinter to try and click a button that has no attributes. The html code in developer tools is
<div class="numeric">
<button>0</button>
<button>1</button>
<button>...</button>
<button>9</button>
</div>
that executes a jquery.
My code looks like this
browser.find_by_css("numeric").find_by_text("0").first.click()
However I am getting that numeric is not found. I have also tried #numeric as well.

That's not really the css for the div class.
You could try to find it by xpath using this:
browser.find_by_xpath("//div[contains(#class, 'numeric')]")

Related

Click checkbox with python selenium

so I wanted to click a checkbox on website using selenium (python).That's the button I want to click
So I thought that it would work with that code:
driver.find_element_by_xpath("//input[#name='termsCheck']").click()
But that gives me an errorThat's the error I get
Additional info: there are 2 more checkboxes on the same page which have also <span class="custom-checkbox"> ::before ::after </span>
Has anyone an idea how to get selenium to click the checkbox?
I have seen some scenarios were the element must be clicked with javascript because it is covered by other elements. Alternatively you could click the <span> element that is covering it.
Here is how to click the element with javascript using python and selenium. Since you have not provided the HTML I am assuming that the xpath you provided uniquely identifies the element you want to click.
element_to_click = driver.find_element_by_xpath("//input[#name='termsCheck']")
driver.execute_script("arguments[0].click();", element_to_click )
On most browsers you should be able to copy the XPath or CSS selector by right clicking the specific element on the developer tools console. The click() method should work.
The code is attempting to click the checkbox and Selenium API doesn't like that. The error informs about that, but is not specific enough. Try using auxiliary class Select instead:
from selenium.webdriver.support.ui import Select
element = driver.find_element_by_xpath("//input[#name='termsCheck']")
select = Select(element)
select.select_by_index(index)
Additionally, make sure that XPath //input[#name='termsCheck'] is only matching single element.
Refer to Selenium Python documentation for more details.

Selenium with Python: can't select inside <!DOCTYPE ... Transitional//EN>

I am new to Python and Selenium and trying to web scrape a page with electoral data (https://historico.servel.cl/SitioHistorico/index2008_alca.htm, html code in picture below). On this page I need to navigate using Selenium since the url doesn't change while selecting items in the menu. As a first step I need to select the "División Geografica" button in the top left corner. I tried to navigate to the corresponding tag using find_element_by_xpath().
However, navigating by xpath only works for tags outside the #document and <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> lines. As an example, driver.find_element_by_xpath("/html/frameset[1]/frame") gets me to the first tage just before #document, but I am unable to select any of the <body>, <form>' or tags that come after. I also tried selecting elements using the name of a tag, e.g. driver.find_element_by_name("DATOS"), but without success. Is this because of the `' and/or '#document' tags/lines or what is the issue here?
The object I am interested in is <a href="geografico.htm" .... </a>. Do you have any suggestions how I can select it using xpath (or any other method)?
Thanks a lot for your help!
best,
likeat.100
source code
This element <a href="geografico.htm" .... </a> is in a frame. You need to switch the focus/control of your webdriver to interact with it.
for switching the control of WebDriver you can use this code :
driver.switch_to.frame(driver.find_element_by_name('guiaFrame'))
then you can easily interact with the mentioned web element.
Remember it is always a good practice if you switch back the focus of WebDriver to it's parent/default once you are done with the current frame , for that you have to use :
driver.switch_to.default_content()
HTH !!

Website submit button and python, no action attribute

I am building a program in Python that interacts with an online store. So far I am able to find the desired item and navigate to the page using BeautifulSoup, but I am having issues clicking the "Add to cart" button. Most of the solutions I've found online using robobrowser and similar would work except that they are dealing with the tag which has a method attribute. The for on the site I am dealing with looks like this:
<input class="button" name="commit" type="submit" value="add to cart">
How would I go about "clicking" this button? What libraries would I need. I'm using python 3 by the way so I can't use mechanize. Thanks in advance for the help.
You can consider using Selenium in Python.
Please use the code snippet below as a reference:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("url")
button = driver.find_element_by_css_selector("input[class='button']")
button.click()
In case you get multiple matches, you can narrow it down by involving more attributes:
button = driver.find_element_by_css_selector("input[class='button'][name='commit']")
Please refer to this link for more examples on Python Selenium.
http://selenium-python.readthedocs.io/locating-elements.html

Click a href button with selenium and python?

I have one button from one LinkedIn page with this code:
<div class="primary-action-button"><a class="primary-action label" href="/requestList?displayProposal=&destID=39959446&creationType=DC&authToken=Yr4_&authType=OUT_OF_NETWORK&trk=vsrp_people_res_pri_act&trkInfo=VSRPsearchId%3A2998448551382744275729%2CVSRPtargetId%3A39959446%2CVSRPcmpt%3Aprimary">Send InMail</a></div>
Is there any way to click on an element just by its href link? Thanks
Using selenium you could use the following code:
driver.find_element_by_link_text("Send InMail").click()
The above answer driver.findElement(By.linkText("Send InMail")).click(); is in Java. In python, use find_element_by_link_text:
driver.find_element_by_link_text('Send InMail').click()
or something like this is sometimes helpful
driver.find_element_by_partial_link_text('Send').click()

selenium: <a> tag with href click behavior wrong

My testing website has an element like this:
评论
There is some javascript hooked with the tag and the desired behavior is when clicking tag, the browser will not go anywhere but stay in the same page, open a comment textarea for user to input.
But in selenium, both happened, the textarea is opened and the browser will navigate to the page that href points to, which is not desired result for my selenium scripts.
How to avoid this?
I am thinking of changing the href attribute to "#" to avoid such problem but looks like the selenium itself can't change the element in the webpage, is that true?
My python selenium script:
ask = driver.find_element_by_xpath("//a[#class='comt']")
ask.click()
As the above users's commented ...since a JS is hooked with the click event over the hyperlink.
Then it's suggested to fire the JavaScript Event directly rather than focusing on click event over hyperlink..
From above url you commented, I took the following element
<a title="" onmousedown="MI.Bos('btnVaryEntrance1')" onclick="vary('yilanyeh','叶怡兰');" href="javascript:void(0)">
It had a JS function vary('yilanyeh','叶怡兰') hooked over Click event...so My code looks like this (C# Code)
IWebDriver driver = new InternetExplorerDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
driver.Navigate().GoToUrl("http://t.qq.com/yilanyeh");
IJavaScriptExecutor jr = (IJavaScriptExecutor)driver;
jr.ExecuteScript("vary('yilanyeh','叶怡兰');");

Categories