How to click this button with python & selenium - python

I'm looking to click the button highlighted in the screenshot below; have tried with pyautogui but found results to be inconsistent so trying selenium instead.
I'm having trouble identifying the button to then call the click function.
Here's the HTML
Alternatively perhaps I could run the 'ng-click' function, unsure how to approach that. If I do this, I'll need to pipe through 'index', from what I can tell from this HTML (my understanding of HTML is minimal)
Thank you

You can have browser to figure out the button CSS selector for you.
Here's how to do that in Chrome:
Open your page in Chrome
Right-click on your button and select the Inspect Element from the context menu
The Inspector window will open with the button's HTML markup selected.
Right-click the selected HTML line(s) and select Copy -> CSS Selector.
Paste the CSS selector into the code below
And here's the code to click your button:
from selenium import webdriver
browser = webdriver.Chrome('/path/to/chromedriver')
browser.get('your/website/url')
button = browser.find_element_by_css_selector('paste the CSS selector here')
button.click()
Hope this helps.
PS: Here's an excellent article (a chapter from the Automate the boring stuff with Python book) on web scraping and browser automation using BeautifulSoap and Selenuim.

Try for xpath as:
//div[#id='channel']//div[#class='channel-list']/div/div/div[#class='ch-btn play']
or
//div[#id='channel']//div[#class='channel-list']//div[#class='ch-btn play']
Let me know if this Answers your Question.

You'll probably want to use CSS Selectors, as they are preferred in selenium over Xpath. Some important notes about html and selenium:
Html is a static language. There is not way to "call" or "run" things in it. That requires use of a different language, like JavaScript.
Selenium mimics an actual user, so selenium is not directly "calling" anything, it is interacting with the page, and the page responds.
python code:
driver = webdriver.Chrome('path/to/chromedriver')
driver.get('your/site/here')
# This is a css selector for the div that you want to click on.
css_selector = "div[ng-click='play($index)']"
# This finds the object that is located at css_selector
button_element = driver.find_element_by_css_selector(css_selector)
# Sends a mouse click to the button_element
button_element.click()

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.

With selenium on python don't know how to shut down a banner which is preventing selenium from accessing the page content

I'm trying to open a site with Selenium (with Python) using Chrome browser, but when I do, a full screen promo banner immediately pops-up and I can't access the site content unless I close it.
On the top right there is an "x" as if it was a quit button, but actually it's an element ::before
and from its description it seems to me that it doesn't contain any button element.
If I operate manually, both clicking on the x and on the upper part of the page outside the banner, the latter closes, but I really don't understand how to access it with selenium.
The webpage I'm trying to open is https://sports.bwin.it/it/sports
Needless to say I'm quite inexperienced, so I hope this question won't sound too basic, but I wasn't able to find a solutione in the selenium docs or on the web; if someone could give me any hint I would appreciate it.
This is a screenshot from the page I'm talking about
This is part of the html code from the web page; the element I am talking about is the one pointed by the arrow;
Based on your screen shot the xpath you want to use would be something like this:
//*[#data-id='dj_sports_c_ovl_br']//span
full code would be something like this:
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//*[#data-id='dj_sports_c_ovl_br']//span"))
)
element.click();

clicking on button no ID in Selenium python

Hello I am trying to click on a button within python selenium that has no ID. The button has the text Submit an application within it. However there are multiple phrases of "Submit an application" on the website. The attributes of the website are:
<a class= "button" href = "(a link)" analytics-on="click" analytics-category="Body" analytics-event="(a link)" analytics-label="Submit an application||(a link)">
According to the documentation, there are multiple methods you can use to locate an element.
In this situation, I would personally use Xpath if available. Open the chrome developer tool on the HTML tab and find the element you want to locate (or right click on the element and go to inspect element). Right click on the HTML tag (in the dev tool) and select copy > xpath.
Now in your python code you can locate your element like so:
button = driver.find_element_by_xpath("past_the_xpath_here")
Alternatively because you are trying to locate a link, you could also locate the element using the link text:
button = driver.find_element_by_link_text("Submit an application")
you can try with css_selector.
driver.find_element_by_css_selector('[analytics-on="click"]').click()
driver.find_element_by_css_selector('[analytics-category="Body"]').click()
or link_text method is also a good choice in locating an a tag,
driver.find_element_by_link_text("Submit an application").click()

python selenium youtube like button

So I am trying for over an hour now to make selenium click the youtube like button...
Nothing works on google and I have no idea what to do anymore.
If someone can help me that would be amazing
(Im kinda new to python)
Thanks!
So following on my comment, you need to inspect the xpath of the youtube like button.
Right click on the like button, press inspect element. It should show a console and in the console the like button path should be highlight. Click on it then click copy xpath.
After that do what you need to do, e.g like_button_click = driver.find_element_by_xpath('xpath of the like button').click()
Selenium docs: https://www.seleniumhq.org/docs/
The below is tested and working - remember that to like videos, you always need to be logged into your Youtube account.
1) Identify the buttons xPath:
To extract the specific Like button xPath, you can inspect the button through any developer browser tool, right click on it and select Copy xPath:
Then just copy the xPath into here:
button = driver.find_element_by_xpath("here")
2) Log into Youtube and click on the Like button
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
driver = webdriver.Chrome()
youtube = driver.get("https://www.youtube.com/watch?v=9fHt-VVG_hg")
sleep(5)
button = driver.find_element_by_xpath("//*[#id='top-level-buttons']/ytd-toggle-button-renderer[1]/a")
ActionChains(driver).move_to_element(button).click(button).perform()
Try this one
//yt-animated-icon[contains(#animated-icon-type,"LIKE")]

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