how to click button in python selenium? - python

<div class="gift">
<button type="button" data-log-actionid-label="gift" data-log-body="{"current_product_no" : "1003072802", "group_product_no" : "", "group_product_firstview_no" : "", "product_price" : "5600", "send_impression": "Y"}" data-is-send-log="true">
togift
<span class="ic_new">new</span>
</button>
</div>
Can't use by text, because this page have lots of text togift
How can I click this button ?

You can try using the class name:
div = driver.find_element_by_class_name('gift')
btn = div.find_element_by_xpath('.//button')
btn.click()
If there are other tags with the same class name, then you can use the css selector or the full xpath

Should click on the div class gift with button.
driver.find_element_by_xpath("div[class='gift']/button").click()

Not enough html source to let us know how to write a correct and short element's location statement.
So just try the below xpath expressions:
"//div[#class='gift']/button[contains(text(),'togift') and ./span[contains(text(),'new')]]"
"//div[#class='gift']/button[contains(text(),'togift')]"
"//div[#class='gift']/button"

Related

Can't Click Radio Button in Selenium Using Python

Here is the HTML Code:
<div class="ui-helper-hidden-accessible">
<input type="radio" name="group2" value="yes">
</div>
Full script
Here are the code which i've tried to click the radio button:
driver.find_element(By.CSS_SELECTOR, 'input[name= "group2"][value="yes"]').click()
AND
rdbutton = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[name= "group2"][value="yes"]'))).click()
AND
rdbutton = driver.find_elements(By.XPATH, "//input[#name='group2']")
for rdbuttons in rdbutton:
if rdbuttons.is_selected():
pass
else:
rdbuttons.click()
It seems that you are using wrong locator. From the HTML you have given i think you are trying to click on radio button that have label Ya if this is the case then try with below xpath:
"//label[contains(text(),'Ya')]"
OR
"//*[normalize-space()='Ya']"
If this does not solve your problem, so please explain and add HTML of proper element either by screenshot or in textual format

How to find selector for this HTML snippet

How to find selector from this? I want to click the button but unable to find selector.it will be a css selector or class selector.
HTML
<button data-bb-handler="confirm" type="button" class="btn btn-success">Continue and Logout Other User</button>
You can try it
button_Login = driver.find_element_by_css_selector("#button").text("Continue and Logout Other User")
Make sure the button is visible when you try to click it.
The following selectors should work:
.btn-succes
//button[#class='btn btn-succes']
You can use the following selectors -
driver.find_element(By.CSS_SELECTOR,'button.btn.btn-success')
driver.find_element(By.CSS_SELECTOR, 'button[class='btn btn-success']')
driver.find_element(By.XPATH, "//button[normalize-space()='Continue and Logout Other User']")

How to click on div class element

I would like to click on a div element that has a custom ID (in this example 12257237).
Here is the element :
<div class="product-params-oem__title product-params-oem-title-js ga-click" data-article-id="12257237" data-gac="OEM_info" data-ga-action="detail">
<span>Afficher les numéros OEM du produit</span>
</div>
I have tried this but without success:
python_button = driver.find_element_by_id("12257237")
python_button.click() #click fhsu link
Thanks for your help
12257237 is not id attribute, it's data-article-id attribute. Those are two different things. You can use css_selector
button = driver.find_element_by_css_selector('[data-article-id="12257237"]')
Or xpath
button = driver.find_element_by_xpath('//div[#data-article-id="12257237"]')

How can i click the element on webpage using selenium with python

HTML information is :
<a title="Create an Account" class="button" href="http://demo.magentocommerce.com/customer/account/create/">
<span>
<span>Create an Account
</span>
</span>
</a>
Create an Account
I am trying with :
create_account_button = driver.find_element_by_xpath("//button[#title='Create an Account']")
create_account_button.click()
but it is not working
Actually assign the WebElement variable, u use the following code
WebElement button = driver.findElement(By.xpath("//a[#title='Create an Account']");
button.click();
Steps
Creating a WebElement variable button and assigning the value to it.
Performing click() on that webelement
For Java follow above steps, for Python use below
driver.find_element_by_xpath('//a[#title='Create an Account']').click()
Xpath you need To use be in <a> not in <button>
So try this Xpath //a[#title='Create an Account']

How to automate webpage button control using Selenium?

How to automate webpage button control like having span child by using
driver. findElement_by_xpath ()
on Ubuntu Linux and chromium Autotest
The wesite is: https://www.youtube.com/my_webcam?privacy=public
I would like to click on Start recording button.
HTML source is:
<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-dark yt-uix-button-has-icon webcam-record-button" type="button" onclick=";return false;" id="record-button">
<span class="yt-uix-button-icon-wrapper">
<span class="yt-uix-button-icon yt-uix-button-icon-upload yt-sprite">
</span>
</span>
<span class="yt-uix-button-content">Start recording </span>
</button>
We have tried it by using
driver.find_element_by_css_selector()
driver.find_element_by_id()
driver.find_element_by_xpath ()
However, nothing has worked. Could you please suggest us appropriate solution?
The record button is within an iframe, you'll need to switch that iframe first and then locate the button with your chosen selector.
iframe = self.driver.find_element_by_css_selector("#webcam-container")
self.driver.switch_to_frame(iframe)
record = self.driver.find_element_by_css_selector("#record-button > span.yt-uix-button-content")
record.click()

Categories

Resources