Selenium click on next button - python

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.

Related

How do I click on an item on my google search page with Selenium Python?

Good time of the day!
Faced with a seemingly simple problem,
But it’s been a while, and I’m asking for your help.
I work with Selenium on Python and I need to curse about
20 items on google search page by random request.
And I’ll give you an example of the elements below, and the bottom line is, once the elements are revealed,
Google generates new such elements
Problem:
Cannot click on the element. I will need to click on existing and then on new, generated elements in this block: click for open see blocks with element
Tried to click on xpath, having collected all the elements:
xpath = '//*[#id="qmCCY_adG4Sj3QP025p4__16"]/div/div/div[1]/div[4]'
all_elements = driver.find_element(By.XPATH, value=xpath)
for element in all_elements:
element.click()
sleep(2)
Important note!
id xpath has constantly changing and is generated by another on the google side
Tried to click on the class
class="r21Kzd"
Tried to click on the selector:
#qmCCY_adG4Sj3QP025p4__16 > div > div > div > div.wWOJcd > div.r21Kzd
Errors
This is when I try to click using xpath:
Message: no such element: Unable to locate element: {"method":"xpath","selector"://*[#id="vU-CY7u3C8PIrgTuuJH4CQ_9"]/div/div[1]/div[4]}
In other cases, the story is almost the same, the driver does not find the element and cannot click on it. Below I apply a scratch tag on which I need to click
screenshot tags on google search
Thanks for the help!
In case iDjcJe IX9Lgd wwB5gf are a fixed class name values of that element all you need is to use CSS_SELECTOR instead of CLASS_NAME with a correct syntax of CSS Selectors.
So, instead of driver.find_element(By.CLASS_NAME, "iDjcJe IX9Lgd wwB5gf") try using this:
driver.find_element(By.CSS_SELECTOR, ".iDjcJe.IX9Lgd.wwB5gf")
(dots before each class name, no spaces between them)

How to find an element within the parent of an element with Selenium

My aim is to find an element by text (in this case a button), then navigate to the div that contains this button, then find and click another element within that div (The listing title).
My expected results were that the program would locate an element within the page with the text "Add to cart" (if there is one), navigate to the parent div, then search that div for a class called "title" and click it. The actual behavior is slightly different. The program will locate a button, navigate to its parent but then it appears to just search the whole page for the title element and select the first one.
My question is: How do I search only the div I have stored as a variable for a specific class.
inStockButton = driver.find_element_by_xpath('//button[text()="Add to Basket"]')
inStockButtonParent = inStockButton.find_element_by_xpath('//span/ancestor::div[1]')
productToClick = inStockButtonParent.find_element_by_xpath('//*/div[1]/h3/a')
productToClick.click();
use relative xpaths:
inStockButton = driver.find_element_by_xpath('//button[text()="Add to Basket"]')
inStockButtonParent = inStockButton.find_element_by_xpath('.//span/ancestor::div[1]')
productToClick = inStockButtonParent.find_element_by_xpath('.//*/div[1]/h3/a')
productToClick.click();
Try this if your element is within 50 pixels of your orgin element.
inStockButton = driver.find_element_by_xpath('//button[text()="Add to Basket"]')
driver.find_element_by_xpath('//h3/a').near(inStockButton).click()

Locating nearest Button Selenium 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()

if statement selenium python webdriver

I want to check to see if a certain window is open and if it is than to proceed to log out however if it is not than to open up the widget and then logout out.
The widget is inside of another frame. I don't want to have to switch frames just to see if the widget is open if I do not have to.
My Code:
window=driver.find_element_by_id("DR44")
if window.is_displayed():
userdropdown=driver.find_element_by_id("Menu").click()
logout=driver.find_element_by_id("df456").click()
else:
LaunchMenu=driver.find_element_by_id("launch").click()
bvWidget=driver.find_element_by_id("54353sfd").click()
launch= driver.find_element_by_id("3rfs").click()
userdropdown=driver.find_element_by_id("userMdfd243l").click()
logout=driver.find_element_by_id("efdf343").click()
My error: Unable to locate element
I want to check to see if the title of the widget is found on the page if so then proceed to log out and if not open up the widget and then log out.
You can not interact any element inside an iframe if you dont switch to iframe. First switch to iframe by driver.switch_to_frame() and you also dont need to assign an element to variable to click to. see below:
driver.switch_to_frame(iframe)
if driver.find_element_by_id("DR44").is_displayed():
driver.find_element_by_id("Menu").click()
driver.find_element_by_id("df456").click()
else:
driver.find_element_by_id("launch").click()
driver.find_element_by_id("54353sfd").click()
driver.find_element_by_id("3rfs").click()
driver.find_element_by_id("userMdfd243l").click()
driver.find_element_by_id("efdf343").click()
To be noted: iframe is a list of iframe ids

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