Selenium - Can't click link having onclick event - python

So this is the link I'm trying to click:
<a class="nav-link text-light" href="#" onclick="return hydra.dissent.selectCommentsTab(event, 'top', {"url":"https://www.some-website.com/","v":"begin","uid":"5xxxxxxx5f2bc7d4b7","s":"controversial","p":1,"cpp":10});">Top</a>
I fetched it using driver.find_element(By.CSS_SELECTOR, "a.nav-link.text-light") and verified it by using get_attribute("innerText") and it did show Top.
But when I try .click() on it, nothing seems to happen.
I also tried .send_keys(Keys.ENTER) and that didn't work either.
Then I tried driver.execute_script("<onclick_text_here>"); still no luck.
The code doesn't throw any error either, so I don't know what the issue is.
Any help would be appreciated.

href="#"
basically means even if you click, it won't redirect you anywhere.
and text also says Top, may be when you click it scroll till Top which can be achieved by many ways in Selenium.

Related

Selenium get URL of "a" Tag without href attribute

I'm facing an element like:
<li _ngcontent-bcp-c271="">
<a _ngcontent-bcp-c271="">2018</a>
<!---->
<!---->
</li>
This element is clickable but since it does not have a href attribute, and I think it should use some script for the click event, I don't have a solution to get the URL from this element.
The code that I use most of the time is as follows:
driver.find_element(By.TAG_NAME, 'li').find_element(By.TAG_NAME, 'a').get_attribute('href')
Update:
I need to know the URL before I click on the bottom.
The answer is: NO, you can not do that.
You can not get the URL before clicking such elements since URL is dynamically created by script etc, it is not statically kept on the page.
The easiest way to know its URL is to click it and then get page's url with:
driver.current_url
Another way is to get the javascript of this page and find in it the code that is responsible for clicking on this link and get the url from it if it is written explicitly there.

I cannot find a "button onclick" element using Selenium and Python

I am automating a process using Selenium and python. Right now, I am trying to click on a button in a webpage (sorry I cannot share the link, since it requires credential to login), but there is no way my code can find this button element. I have tried every selector (by id, css selector, xpath, etc.) and done a lot of googling, but no success.
Here is the source content from the web page:
<button onclick="javascript: switchTabs('public');" aria-selected="false" itemcount="-1" type="button" title="Public Reports" dontactassubmit="false" id="public" aria-label="" class="col-xs-12 text-left list-group-item tabbing_class active"> Public Reports </button>
I also added a sleep command before this to make sure the page is fully loaded, but it does not work.
Can anyone help how to select this onclick button?
Please let me know if you need more info.
Edit: you can take a look at this picture to get more insight (https://ibb.co/cYXWkL0). The yellow arrow indicates the button I want to click on.
The element you trying to click is inside an iframe. So, you need to switch driver into the iframe content before accessing elements inside it.
I can't give you a specific code solution since you didn't share a link to that page, even not all that HTML block. You can see solutions for similar questions here or enter link description here. More results can be found with google search

Python selenium - clicking checkbox that also has a dropdown text

I wish to mark a checkbox, but the problem is that that text near the checkbox also incorporates a collapse-link, so whenever I try and click it, only the drop-down text is shown so the checkbox isn't being marked.
I've tried clicking the checkbox using
driver.find_element_by_xpath(".//*[contains(text(), 'example_text_positiond_next_to_checkbox')]").click()
but this, as mentioned, only clicks the text, which then proceeds to display the collapsed text
the HTML snippet is (and I hope I haven't missed any important part)
<label class="custom-control-label"
for="customCheck1"><a class="collapse-link collapsed"
data-toggle="collapse" href="#collapseInfo" role="button" aria-expanded="false"
aria-controls="collapseExample">example_text_positiond_next_to_checkbox<span
class="arrow"></span></a></label>
I've thought of somehow clicking some margin to the left of the text, but not quite sure how to do so.
any suggestions are welcomed!
that's the box:
driver.get("https://corona.health.gov.il/en/green-pass/")
driver.find_element_by_css_selector('[for="customCheck1"]').click()
input()
you can use any attribute to find element the above locator with attribute 'for' will work. You can use xpath or css
XPATH equalent:
//*[#for="customCheck1"]
This can be done with css selector, as there is no direct way to inspect that checkbox in DOM.
driver.find_element(By.CSS_SELECTOR,"input#customCheck1.custom-control-input").click()
try below css.
input#customCheck1.custom-control-input

How to find the "See More Posts from..." button? Selenium - Python

Disclaimer: sorry if my explanation is weird. It's my first week learning.
As my first project, I've set out to build a program that counts Instagram posts in a given time frame. Almost everything works, but there is one button standing in my way. (sometimes)
When you open up Instagram with WebDriver, the GDPR notice pops up. This is easy to get rid of since the xpath for this button is always the same. Or less barbaric way:
driver = webdriver.Chrome()
driver.get(ig_link)
driver.find_element_by_xpath('//button[text()="Accept"]').click()
then, I want to scroll down and during it, get URLs of all posts on the page. As I've already mentioned, on some profiles this works just fine. Yet on others, only few recent posts load up and then you are required to press this pesky "Show More Posts from {instagram_tag}"
My idea was to just brute-force it through driver.find_element_by_xpath() but then I found out that on different IG pages, the xpath is different. The button also doesn't have any text directly inside it, so I am not able to use the same approach that I've used with the "Accept" button.
<button class="tCibT qq7_A z4xUb w5S7h">
<div class="Igw0E IwRSH eGOV_ _4EzTm lC6p0 HVWg4">
<div class="_7UhW9 xLCgt qyrsm h_zdq uL8Hv l4b0S ">Show More Posts from choco_afro
</div>
</div>
</button>
My idea is that I check whether the div element under button includes text "Show More Posts" and then click the button, but I don't know how to do that if even possible. Would anybody be so kind and help me with this? :)
I believe, that, this xpath might help you.
//button//div[contains(text(), 'Show More Posts')]/../..
If each IG page has the same HTML design, then this example should help you. Solution: Click Show More Posts Button
driver.find_element(By.XPATH, "//button//div[contains(text(), 'Show More Posts')]/../..").click()
Try this:
btn = driver.find_element_by_xpath('//button[#class = "tCibT qq7_A z4xUb w5S7h"]')
div = btn.find_elements_by_tag_name('div')[1]
if 'Show More Posts' in div.text.strip():
btn.click()
You can use the following to retrieve that button from it's sibling's text by css selector.
//div[text()='Show More Posts from choco_afro']/parent::div/parent::button

Selenium can`t find element - "connect" button in LinkedIn

Hey guys I am working on an automating bot for adding connections in LinkedIn, recently I encountered a problem for finding the "Connect" element(button) after searching in the search box and filtering for "People only".
That is the inspect of the relevant part:
<div id="ember374" class="ember-view">
<button id="ember378" class="artdeco-button artdeco-button--2 artdeco-button--secondary ember-view" data-control-name="entity_action_primary" data-control-id="AQT4G2UkQk6LQlbdf6XySw==">
<!---->
<span class="artdeco-button__text">Connect</span></button>
This is my tries for clicking on that "connect" element:
1. CONNECT_BUTTON_SEARCH_PAGE = "//*[contains(#class,'artdeco-button__text') and contains(.,'Connect')]"
2. CONNECT_BUTTON_SEARCH_PAGE = "//*[text()='Connect']"
3. CONNECT_BUTTON_SEARCH_PAGE = "//button[contains(. , ’Connect’)]"
WebDriverWait(self.driver, 5).until(EC.presence_of_element_located(
(By.XPATH, Xpath.CONNECT_BUTTON_SEARCH_PAGE))).click()
I have to mention that it works fine in the "Network" page, but I want it to add connections after searching a key word. So when I'm searching people according to a special key word, in that page I don't find the "Connect" button
I don`t know why but I can not reach for that element, appreciate if someone could help me
Finaly found a (weird) soulution with the help of #arundeepchohan.
First I used this xpath:
"//button/span[text()='Connect']/.."
But then I had to "refresh" the page before trying to click the "connect" button. I don't have an explanation for that to work and why it was necessary to refresh the page, but I used this part of code before the clicking method:
time.sleep(2)
self.driver.get(self.driver.current_url)
Thanks for everyone who tried to help!
Try this xpath :
//button[contains(. , ’Connect’)]

Categories

Resources