How to click automatically on "Content" button - python

This is element HTML:
<button class="search-vertical-filter__filter-item-button button-tertiary-medium-muted" data-ember-action="" data-ember-action-5975="5975"> Content </button>
Code:
driver.find_element_by_link_text('Content').click()
Even i also tried:
driver.find_element_by_class_name('search-vertical-filter__filter-item-button button-tertiary-medium-muted').click()
I want to click on "Content" button on LinkedIn automatically and I am using Selenium and Python for that.

Try to use the following code:
driver.find_element_by_css_selector(".search-vertical-filter__filter-item-button.button-tertiary-medium-muted").click()
Hope it helps you!

As per the HTML provided to click on the button with text as Content you can use the following line of code :
driver.find_element_by_xpath("//button[#class='search-vertical-filter__filter-item-button button-tertiary-medium-muted' and normalize-space()='Content']").click()

Related

Which Selenium method or command can be used to click on the following buttons?

Which method in Selenium with python can be used to click on the following buttons?
Button nr. 1:
<a href="/help.html"> = $0
<i class="fa fa-question" aria-hidden="true"></i> HELP </a>
Button nr.2:
<a class="active-menu-item" href="/help/making_service_requests.html">Service Requests</a>
Button nr.3:
Transfer Service
I have already tried but wont work:
driver.find_element_by_xpath('//*[#id="topmenuheader"]/div/ul/li[3]/a').click()
driver.find_elements_by_partial_link_text("Transfer Service").click()
driver.find_element_by_class_name("btn").click()
I have encountered many instances where there are no id, no class etc, butand href available or an href with an html reference as mentioned above. These seem to be tricky as i have tried quite a few things. Maybe some of you have encountered these and could help out. Thanks
These should work for those 3 elements. I showed multiple ways of getting those a tags either by href, class or text.
driver.find_element_by_xpath("a[#href='/help.html']").click()
driver.find_element_by_xpath("a[#class='active-menu-item']").click()
driver.find_element_by_xpath("a[text()='Transfer Service']").click()
You can do this :
Driver = driver.find_element_by_xpath("path here")
Driver.click()

How to check checkbox using Selenium in Python

I have a problem with this checkbox. I tried to click searching element with id, name, XPath, CSS Selector and contains text and still I could not click on this checkbox. Additionally, I've tried with another site with similar HTML code and on this site, it was enough to look for id and click. Any ideas?
<div class="agree-box-term">
<input tabindex="75" id="agree" name="agree" type="checkbox" value="1">
<label for="agree" class="checkbox-special">* Zapoznałam/em się z Regulaminem sklepu internetowego i akceptuję jego postanowienia.<br></label>
</div>
Here is my Python code https://codeshare.io/5zo0Jj
I have used javaScript Executor and it clicks on the element.However I have also checked webdriver click is not working.
driver.execute_script("arguments[0].click();", driver.find_element_by_id("agree"))
I don't know why this is, but in my experience some boxes don't accept click but do accept a 'mousedown' trigger.
try:
driver.execute_script('$("div.agree-box-term input#agree").trigger("mousedown")')
This solution does rely on jquery being on the page, if it's not we can write it in javascript
r = driver.find_element_by_xpath("//*[#id="form-order"]/div[2]/div[4]/label")
r.click()
Does this work for you? Sometimes it's just a question of selecting the right xpath, or adding the brackets after click.
Does your code contain nested html tags? For example:
<html>
<div>
<p> Some text </p>
<html>
That block can't be traversed!
</html>
</div>
</html>
Anything inside the second HTML tags can't be traversed/accessed. Try to see if that's the case.
In any other case the following code ran perfectly fine for your snippet:
driver.find_element_by_css_selector('#agree').click()

How to click on the button with text as Search as per the html through Selenium and Python?

I am trying to use Selenium to click a "Search" Button but I cant seem to locate it.
<div class="search">
<input type="submit" title="Search" value="Search" class="spinner">
</div>
My code looks like this:
search_button = driver.find_element_by_class_name("spinner")
search_button.send_keys(Keys.RETURN)
Any help would be much much appreciated. Thank you
As per the HTML you have shared to invoke click() on the button with text as Search you can use either of the following solutions:
Using click():
driver.find_element_by_xpath("//input[#class='spinner' and #title='Search']").click()
Using submit():
driver.find_element_by_xpath("//input[#class='spinner' and #title='Search']").submit()
You can try like this
driver.find_element_by_css_selector(".spinner")
driver.find_element_by_xpath("//div[#class='search']/input")
driver.find_element_by_xpath("//input[#type='submit' and #title='Search']")
driver.find_element_by_xpath("//input[#type='submit' and #value='Search']")
driver.find_element_by_xpath("//input[#title='Search']")
driver.find_element_by_xpath("//input[#value='Search']")
elementByXpath = driver.find_element_by_xpath("//div[#class='search']")
elementByXpath.find_element_by_tag_name("input").send_keys(Keys.RETURN)

Web Scraping with Python: Iinput text and click a button

I was doing some web scraping with python (Linkedin site) and got stuck with the following 2 issues: 1) How do I input text on a search bar? 2) How to click a button? First, this is the search bar code:
<input aria-autocomplete="list" autocomplete="off" spellcheck="false"
placeholder="Búsqueda" autocorrect="off" autocapitalize="off" id="a11y-
ember6214" role="combobox" class="ember-text-field ember-view" aria-
expanded="false">
To input the text I was using the xpath (and it works) but it changes every time I login into the site:
search = driver.find_element_by_xpath('//*[#id="a11y-ember997"]')
search.send_keys('MedMake')
So could I use instead part of the input bar code above so that I can rerun my script multiple times?
My second point is 2) how to click a button. Again I was using the xpath but it changes after every login. My code was:
button = driver.find_element_by_xpath('//*[#id="nav-search-controls-wormhole"]/button')
button.click()
I inspected the button code and I would instead like to use data-vertical="PEOPLE" or any other of this unique fields (the tag button is not enough since there are many buttons on Linkedin site). By the way,how are all these inner fields called? I believe part of my problem arises from the lack of html code understanding.
<button data-vertical="PEOPLE" data-control-
name="vertical_nav_people_toggle" data-ember-action="" data-ember-
action-8620="8620" data-is-animating-click="true">
Gente
</button>
If id attribute values are dynamic you can use other attributes with static values:
search = driver.find_element_by_xpath('//input[#placeholder="Búsqueda"]')
search.send_keys('MedMake')
button = driver.find_element_by_xpath('//button[normalize-space()="Gente"]')
button.click()
First one use xpath
//input[contains(#class,'ember-text-field')]
Second one use the xpath
//button[#class='vertical_nav_people_toggle']

Click a button using selenium

Here is the code for the button
<li class="selected ng-binding" ng-bind="text.loginLink">Click Me</li>
How can I click this type of button. I have tried
button = driver.find_element_by_xpath('//li[contains(text(),"Click Me")]')
but it is not working. What can I do ?
Try this xpath :
//span[contains(text(),"Click Me")]
driver.find_element_by_xpath("//li[contains(text(),'Click Me')]").click()
Try below xpaths, if not please provide the html source for the parent element's as well.
//li[.='Click Me']
//li[contains(text(),'Click Me')]
//li[#ng-bind="text.loginLink"]

Categories

Resources