Locating nearest Button Selenium Python - python

I am trying to click the "Delete Comment" button after finding the comment that contains a specific hashtag, husky, which is a hyperlink.
Since there are multiple "Delete Comment" buttons, I think the best way is to just find the comment that has the hashtag, and then click the nearest button, but I could be wrong there.
In the picture, I want to click the button highlighted below the hashtag, not below:
So far, I have
self.browser.find_element_by_xpath('//a[#href="/explore/tags/husky/"]')
Which successfully locates the tag, but I am stumped after that.

You can use one of xpath below.
Explanation: find a with "#hasky" text, get first parent li with "menuitem" role and get child button (with "Delete Comment" title attribute):
//a[.='#husky']/ancestor::li[#role='menuitem'][1]//button
//a[.='#husky']/ancestor::li[#role='menuitem'][1]//button[#title='Delete Comment']
//a[contains(#href, "/explore/tags/husky/")]/ancestor::li[#role='menuitem'][1]//button
//li[#role='menuitem' and .//a[.='#husky']]//button[#title='Delete Comment']

Something simple like
//a[.='#husky']//following::button[#title='Delete Comment'][1]
should work just fine. If it were me, I would wrap this in a method and pass in the link text to delete the appropriate comment. You can then take the link text and put it into the locator in the place of #husky.
def delete_comment(comment)
driver.find_element_by_xpath(f"//a[.='{comment}']//following::button[#title='Delete Comment'][1]").click()

Related

With Selenium/Python how do I get the page viewable text of a checked radio button (or checkbox)?

I have a form that I am processing where I need to get the text values of all checked radio buttons (one radio button checked per line item of course).
The HTML is like so (for a radio button that is checked):
<div style="white-space:nowrap;margin-right:15px;display:inline-block;">
<input
type="radio"
name="question_id[927]"
id="question_id[927]"
value="276"
class="noborder"
checked="">Blue
</div>
Even though the checked="" attribute value is empty, selenium/python will return correctly that it is checked by calling: is_checked = element.get_attribute('checked'), so that works.
The other radio buttons in the line item, that are not checked, simply leave out the checked="" attribute.
I need to get the word Blue into a variable.
After experimenting with many things, I cannot seem to extract the text beside the radio button.
I am successfully finding the list of radio button 'Element's', iterating through those and successfully finding which one is checked, and now only need to get the text value of the radio button that is checked to store that value in a database.
webdriver.get_attribute() only works with items inside the <input..> tag.
The text I need to get is enclosed by a <div>, and all radio buttons in the list are enclosed by the same <div> text.
I need to do the same with a list of checkbox values, getting the text from each one that is checked, and the construction is the same, with the text just after the <input...> tag and just before the closing </div>
I have searched every selenium / python article I could find, and am given no clue as to how to get this text.
It would be greatly appreciated if anyone anywhere has a working code line/snippet that would work to get this.
thank you,
It seem like you want to get the div tag text from the parent of the current input tag, so try using the parent approach like the following:
elems = driver.find_elements_by_xpath('//input[#checked]//parent::div')
print(len(elems))
for elem in elems:
print(elem.text)
Reference: XPath Axes

How to find href through class name and click with selenium

I'm using Selenium with python to make a spider.
A part of the web page is like this:
text - <span class="sr-keyword">name</span> text
I need to find the href and click.
I've tried as below:
target = driver.find_element_by_class_name('_j_search_link')
target.click()
target is not None but it doesn't seem that it could be clicked because after target.click(), nothing happened.
Also, I've read this question: Click on hyperlink using Selenium Webdriver
But it can't help me because in my case, there is a <span class>, not just a simple text Google.
You can look for an element with class _j_search_link that contains text Wat Chedi Luang
driver.find_element_by_xpath('//a[#class="_j_search_link" and contains(., "Wat Chedi Luang")]')
driver.find_elements_by_partial_link_text("Wat Chedi Luang").click()
I think you didn't find right element. Use CSS or Xpath to target this element then click it, like:
//a[#class='_j_search_link']
or
//a[#class='_j_search_link']/span[#class='sr-keyword']

Selecting a button, list object has no attribute click python selenium

After searching for a while an answer to my question, I couldn't get an answer that helped me, so here I am asking for your help ! :)
Right now, I am trying to select a plan on a website page which, after it has been selected (Read : a certain button clicked) displays the rest of the page where I can send the keys / values that I want to send.
Here is the code I am using
select_plan = browser.find_elements_by_xpath(".//*[#id='PostAdMainForm']/div[1]/div/div/div/div/div[1]/div[3]/button")
select_plan.click()
I found the xpath with Firepath, but when I run my code it gives me a AttributeError: 'list' object has no attribute 'click'
Here is the page I am trying to click from
https://www.kijiji.ca/p-post-ad.html?categoryId=214&hpGalleryAddOn=false&postAs=ownr
(I am looking to click on the left button, the one in blue)
Thank you very much for you help :)
The method find_elements returns a list, not a single element. You are taking the result and trying to click on it. Like the error says, you can't click on a list.
Either use find_element (singular) or use find_elements (plural) and then click on one of the elements that was returned.
# using find_elements
select_plans = browser.find_elements_by_xpath(".//*[#id='PostAdMainForm']/div[1]/div/div/div/div/div[1]/div[3]/button")
if len(select_plans) > 0:
select_plans[0].click()
# using find_element
select_plan = browser.find_element_by_xpath(".//*[#id='PostAdMainForm']/div[1]/div/div/div/div/div[1]/div[3]/button")
if select_plan:
select_plan.click()
Though, the link for the page you shared did not have the blue button. However, I have found it, after navigating to 'Post Your Ad' page. You can click on the Select button which is in blue color using the text appearing before it. For example, using text Basic, you can reach to the Select button. Following code shows, how we can achieve this:
select_plan = browser.find_element_by_xpath("//h3[text()='Basic']/following::button[text()='Select'][1]")
select_plan.click()
Let me know, whether it works for you.

Selenium click on next button

How can i get selenium to click on the next button from the HTML code below?
<span class="action-btn" data-bind="visible: viewModel.page() < viewModel.pages(), click: viewModel.changePage(+1);">></span>
Initially I wrote:
elm = driver.find_element_by_class_name('action-btn')
elm.click()
time.sleep(4)
But I noticed that in the website, there are other buttons with similar names as well. Such as action-btn customize and action-btn right
With my current code, its basically just clicking on action-btn customize and my guess is because this button name comes before the code that I intended to click on.
How should I be writing my code instead?
Update
Here is a screen shot of the frame. The yellow highlight is what I am trying to click on.
Looking at your screenshot it seems the action-btn you want to click is the last one in pager class.
And, if you have only one div element with class pager, you could select all action-btn inside it and get the last one:
elem = driver.find_element_by_css_selector("div.pager span.action-btn:last-of-type");
Here are more details about :last-of-type selector:
The :last-of-type CSS pseudo-class represents the last sibling with the given element name in the list of children of its parent element.

How to send keys to a difficult element in selenium (Python)

I am trying to click a small button, which has no "ID" or "Name", on a website. The only unique identifier is onclick, which is as follows:
onclick="submitForm('DefaultFormName',1,{'param1':'HXCTIMECARDACTIVITIESPAGEXgm7J5oT','serverValidate':'1uCdqvhJe','param2':'',event:'details|1'});return false;"
However, another button on the page has the following onclick:
onclick="submitForm('DefaultFormName',1,{'param1':'HXCTIMECARDACTIVITIESPAGEXgm7J5oT','serverValidate':'1uCdqvhJe','param2':'',event:'details|2'});return false;"
The only difference is a 1 vs. a 2 in the middle of the code. I tried to use a "find_element_by_css_selector" with the following code but it didn't work:
driver.find_element_by_css_selector(".x1p[onclick*='details|1']").click()
Another option would be selecting the preceding element, with the following code:
precedingbutton = driver.find_element_by_name('B22_1_6')
And then sending tab and then enter. However, after I send Tab, I don't know of a way to send Enter without assigning the Send_Keys command back to the preceding box, which deselects the button I want.
Let me know if you can help!
If you can locate the parent, you can locate the child, either with xpath or the nth-child css selector, relative to it.
Edit in response to comments:
Assuming by "2 elements away" you mean it is a sibling preceding the target, try something like td[name="B22_1_6"] + td + td. This is the sibling selector.
You can do it easily by
driver.find_element(By.XPATH, 'your xpath').click()
For finding the xpath , just inspect the button with firebug, and in html tab right click on selected element and click on Copy XPath.
Hope it will help you.

Categories

Resources