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()
Related
I'm trying to click this button on this website. To get to it you start on https://fantasy5.com/football and once on the website you click on the login/signup button top right. then you are on the correct link and the button I am trying to press is the terms and privacy button in the signup section.
Any advice on what code I should use to find it would be much appreciated.
If you are trying to select that checkbox, use this xpath - //div[contains(#class,'Checkbox')][1]. Below line worked for me.
driver.find_element_by_xpath("//div[contains(#class,'Checkbox')][1]").click()
we have unique id terms
did you try :
driver.find_element_by_id("terms").click()
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.
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()
Sign in
Hey Guys,
Pretty simple question here but I'm having trouble with it for some odd reason. What is the best way to "grab" this button in selenium (using python but a generalized solution would work as well). Its a button, but its html is not a button which means this has not been working for me.
driver.find_element(By.XPATH, '//button[text()="Sign in"]')
Any help would be great, thanks!
You should be able to access it using the a tag instead of button like this
driver.find_element(By.XPATH, "//a[contains(text(), 'Sign in')]")
Selenium docs on locating elements by XPath
and
A good SO answer to help locate an element that contains given text
There is a website with an image button on it which I am trying to click using selenium. The code for this from inspecting the element is:
<img src="../images/aaa.gif" width="100px" height="100px" border="0" alt="aaa">
I know how to use find_element_by_name, id, etc...but am unsure how what to use to click the above. Could anyone advise please?
PS. The image also has a href on a seperate line of code if that makes things simpler?
It the image is always exactly the same, try this xpath:
//img[#src="../images/aaa.gif"]
If the image always has the same ALT attribute you can also use the CSS selector, img[alt='aaa'].