How to let selenium edit an element - python

I currently am writing a script for web automation but I want the program to edit an element for me here's a sample of the code I'm using
final = driver.find_element(By.XPATH,"//button[contains(text(),'Change profile name')]")
final.click()
now it won't click it since the website has the element set it false so I want selenium to edit and make the element true so it can click it, is that possible by any chance and if so can anyone explain and thanks!
Heres what the elements HTML look like
button class="btn btn-disabled" type="submit" data-testid="ChangeNameButton" aria-describedby="changeNameFormError" aria-disabled="true" data-bi-type="button">Change profile name
so instead of btn disabled, I want it to change it to enabled then click it and thanks in advance for any help submitted :)

The button you are trying to click is disabled and you can enable it with JS.
Something like this:
final = driver.find_element(By.XPATH,"//button[contains(text(),'Change profile name')]")
driver.execute_script("arguments[0].setAttribute('className', 'btn-enabled');", final)

To change the text of an element do something like the following:
driver.execute_script("document.getElementById('theelement').innerHTML = 'changed text';");
To change or set attribute of element:
element = driver.find_element_by_class_name("NAMEOFELEMENT");
driver.execute_script("arguments[0].setAttribute('color: blue;')", element);

Related

Tagging an HTML element in Python not working

I have a script going using Selenium and on a new window pop up I am trying to retrieve this element:
<div class="custom-control p-2">somenumber</div>
The full path to this class is
html/body.modal-open/div#modalUserPRMLead.modal.fade.show/div.modal-dialog/div.modal-content/div.modal-body/div.row.justify-content-center.p-3/div.col-md-12/div.custom-control.p-2
I have tried variants of these to grab just the text of 'somenumber'
driver.find_element(By.CSS_SELECTOR,'row justify-content-center p-3').get_attribute('innerHTML')
driver.find_element(By.XPATH("html/body.modal-open/div.col-md-12./div.custom-control.p-2")).text
driver.find_element(By.XPATH("custom-control p-2")).text
driver.find_element(By.XPATH("div.row.justify-content-center.p-3")).text
driver.find_element(By.XPATH,custom-control p-2).text
driver.find_element(By.XPATH,div.custom-control.p-2).text
The errors I get is that it cannot locate the tag from current session. It's a new window that pops up as a result of previous script, I did driver.current_url just to make sure it was capturing the link to the new window in current session and it was.
I'm not understanding how to index down to the div class and just grab the text. Any help would be greatly appreciated. The script prior to window pop for form input was much easier because I could tag everything using By.NAME or By.ID
Your CSS selector is wrong. XPaths don't really looks like XPaths...
Try
driver.find_element(By.XPATH, '//div[#class="custom-control p-2"]').text
or
driver.find_element(By.CLASS_NAME, 'custom-control').text
In case element is dynamic you might need to apply wait
You can also check basic syntax of XPath and CSS-selector

interact elements with dynamic label in python selenium

Python newbie here. All of 1 month old. Most of the elements in page i am trying to scrape, i am able to handle them and interact with them. there are two elements that have dynamic labels which i am not able to handle. The source page looks as below
<span class="a-button-inner">
<input class="a-button-input" type="submit" aria-labelledby="Ae8MCi-55">
<span id="Ae8MCi-55" class="a-button-text" aria-hidden="true">Add Your Key</span>
</span>
Upon each refresh the label is new so i cannot get a fixed XPATH of that. There are multiple items with span class "a-button-inner" as well as input class "a-button-input" and there are other submit buttons as well in the rest of the page. The only thing unique is the Span text "Add Your Key".
Appreciate all help to get the submit button element and to click/submit it.
from selenium import webdriver
.
.
.
.
# objAddKey = driver.find_element_by_xpath('//*[#id="Ae8MCi-55"]/span/input') does not work as second round its a different XPath
objAddKey = driver.find_element_by_link_text('Add Your Key') # hoping this will get a sibling and then to get the parent and then look for all the children. I don't even know if its possible in python.
objAddKey.click()'
Tried to search for searching by span text, and came across some other stuff,
https://www.tutorialspoint.com/how-to-get-text-found-between-span-selenium
WebElement l = driver.findElement(By.xpath("//p/span"));
String s = l.getText();
but this does not help either.
https://selenium-python.readthedocs.io/locating-elements.html
went through this, but not much help either.
Appreciate your help and pointers.
Thank you.
If Add Your Key is unique then you can use the below xpath :
//span[contains(text(), 'Add Your Key')]//preceding-sibling::input[#class='a-button-input']
or even without class :
//span[contains(text(), 'Add Your Key')]//preceding-sibling::input
and use it like this :
objAddKey = driver.find_element_by_xpath("//span[contains(text(), 'Add Your Key')]//preceding-sibling::input")
objAddKey.click()
I would suggest you to have explicit waits for more reliability.
Selenium - Python - Explicit waits

Selenium not clicking element

I have the following checkbox element:
<input checked="checked" class="iceSelBoolChkbx"
id="mainContentId:newSerchCheckBox" name="mainContentId:newSerchCheckBox"
onblur="setFocus('');"
onclick="var form=formOf(this);iceSubmitPartial(form, this, event);"
onfocus="setFocus(this.id);"
onkeypress="var form = formOf(this);
Ice.util.radioCheckboxEnter(form,this,event);"
type="checkbox">
I currently am locating the element and attempting to click it using
check_box = driver.find_element_by_id('mainContentId:newSerchCheckBox')
check_box.click()
When I run the code it runs without any errors but on the site the checkbox is still unchecked. What could be causing this and is their an alternative way to check the box?
Try replacing click with mousedown, I don't know what the underlying difference is but it resolved the issue I was having with click().
driver.execute_script('$("#mainContentId:newSerchCheckBox").trigger("mousedown")')
Let me know if you're thrown an error and we can re-write it in pure Javascript rather than relying on jQuery.
I think your checkbox is already selected if you see the input tag checked="checked".So you need to check first whether selected or not.If selected do not click.
element=driver.find_element_by_id("mainContentId:newSerchCheckBox")
if element.is_selected():
print('Check box is already selected')
else:
element.click()

Cannot locate element using Selenium

I am using Selenium with Python and Chromedriver to click on a "button" (not actually a button in the HTML, but looks like one on screen and is clickable). The HTML is below, I am trying to click on the one with the 'button-new' id.
<td class="read" colspan="4">
Read
<a id="button-new" title="Read new record from database" class="button button-new">New</a>
</td>
I have tried using:
new_button = driver.find_element_by_id("button-new")
new_button = driver.find_element_by_css_selector("A.button.button-new")
new_button = driver.find_element_by_xpath('//*[#id="button-new"]')
followed by
new_button.click()
But I get the NoSuchElementException and it cannot locate it.
I've not had this problem with any other elements and not sure where I'm going wrong.
EDIT with solution:
I realised I needed to switch iframe
driver.switch_to.frame(driver.find_element_by_id("nested1"))
To make sure whether the element is being located try this:
new_button_list = driver.find_elements_by_xpath('//a[#id="button-new"]')
this will return a list because of using 'elements':
then check the length of the list, one good thing is it wont through an exception and also let you know whether the element is located.
try to use it with if statement, maybe they put special a empty button to confuse us,
new_button = driver.find_element_by_id("button-new")
if new_button:
new_button.click()
can you check with find_elements_by_id how many btn are there ?
btns = driver.find_elements_by_id("button-new")
print(btns)
# also we can check with # find_elements_by_id
btns = driver.find_elements_by_id("button-new")
if len(btns):
new_button[0].click()

Cannot Click a button With Selenium

I am trying to automate a process of clicking some buttons. Although I have come really far and only 1 button that is not being able to work . I am using python with selenium. So I just want to click this button but I am unable to do so. Below is my code I have tried with css select and by xpath but Still I am unable to click it , I am getting error path not found.
This is the button that I want to click
<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit" type="button" onclick=";return false;" data-channel-creation-token="GhaqucG9ARAKDi9teV92aWRlb3M_bz1VKAQ%3D"><span class="yt-uix-button-content">CREATE CHANNEL</span></button>
I have tried the following 2 codes but none of them work.
driver.find_element_by_xpath("//button[#class='button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit']").click()
driver.find_element_by_css_selector('button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit').click()
Let's go over your attempts:
driver.find_element_by_xpath("//button[#class='button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit']").click()
This one did not work because you are trying to put a CSS selector into a #class attribute value check. You meant to do something like:
//button[#class='yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit']
driver.find_element_by_css_selector('button.yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit').click()
This one did not work since you are not specifying multiple classes in a CSS selector correctly, classes need to be separated with a dot:
button.yt-uix-button.yt-uix-button-size-default.yt-uix-button-primary.create-channel-submit
Note that a much simpler selector should do the job - you don't have to specify all classes in a CSS selector - pick a more data-oriented and unique one, in this case I think this should be reasonably reliable and readable:
driver.find_element_by_css_selector('button.create-channel-submit').click()
Assuming you want to click on the button CREATE CHANNEL you need to consider the presence of the <span> tag, within the <button> tag. You can use the following line of code:
driver.find_element_by_xpath("//button[#class='yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit']/span[class='yt-uix-button-content']").click()
You can click on your element with next method:
driver.find_element_by_xpath("//span[(#class='yt-uix-button-content') and contains(text(), 'CREATE CHANNEL')]/..").click()
You can try this code
driver.find_element_by_css_selector('button.yt-uix-button.yt-uix-button-size-default.yt-uix-button-primary.create-channel-submit').click();
can you try using by_class_name. You are using xpath and css to look up the class name
driver.find_element_by_class_name('yt-uix-button yt-uix-button-size-default yt-uix-button-primary create-channel-submit')

Categories