I'm trying to automate some processes at work on our inventory site and I'm having trouble inputing text into a simple search box!
Here is the website code:
<td class="GJCH5BMASD" style="">
<input type="text" class="GJCH5BMD1C GJCH5BME1C" style="font-size: 13px; width: 100%;">
</td>
Here is my code:
opens = driver.find_element_by_css_selector(".GJCH5BMD1C GJCH5BME1C").click()
I keep receiving the error:
ElementNotVisibleException: Cannot click on element
Any thoughts?
Well nevermind!
I fixed it by changing:
opens = driver.find_element_by_css_selector(".GJCH5BMD1C GJCH5BME1C").click()
to:
opens = driver.find_element_by_css_selector(".GJCH5BMASD").click()
You need to provide . with every class name using css_selector, you can also try as below :-
opens = driver.find_element_by_css_selector(".GJCH5BMD1C.GJCH5BME1C").click()
Related
I am trying to automate some tasks at work. Requests wont work because I don't have admin access to my works Intercom App. Therefore I use Selenium.
I want to write "Hey" in the chat box of Intercom, and send the message.
** The problem is a changing ember number every time I have a new conversation. It works when I copy the right ember number every time, but when changing the conversation, it doesn't work anymore. **
I am looking for some kind of script to change the ember = XXXXX into the right number each time
Not really relevant to the code problem, but I am using Chrome in debugging mode, to avoid logging in every time I need to test the code, and I am using tkinter to have a button to press, every time I want to write "Hey" in the chat box.
Sorry, I understand it is difficult to replicate this problem.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
#___________
#In order to run Selenium in an already opened browser / session, I need to run this code in CMD:
#cd C:\Program Files (x86)\Google\Chrome\Application
#
#chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\Users\peter\testprogram"
#___________
opt=Options()
opt.add_experimental_option("debuggerAddress","localhost:9222")
driver=webdriver.Chrome(executable_path="
C:\\ProgramFiles\\crromedriver\\chromedriver.exe",options=opt)
def hey():
ember = 32890
hey = driver.find_element_by_xpath('//*[#id="ember'+str(ember)+'"]/div/div[3]/div[1]/div/p')
hey.send_keys("Hey!")
The specific HTML element where I want to write "Hey!": (This is under the big HTML code below)
<p class="intercom-interblocks-align-left embercom-prosemirror-composer-block-selected" style="">Hey! This is where I want my text</p>
One might suggest to use
hey = driver.find_element_by_class_name('intercom-interblocks-align-left embercom-prosemirror-composer-block-selected')
hey.send_keys("Hey!")
But this doesn't work for me.
The HTML element where the ember number is changing:
<div id="ember32890" class="u__relative inbox__conversation-composer__wrapper ember-view"><div>
<div></div>
<div>
</div>
<div data-test-prosemirror-composer="" class="composer-inbox composer-style-basic o__fit conversation__text embercom-prosemirror-composer ">
<style>
.ProseMirror {
outline: none;
white-space: pre-wrap;
}
.ProseMirror .intercom-interblocks-html-block {
white-space: normal;
}
li.ProseMirror-selectednode {
outline: none;
}
.ProseMirror-selectednode.embercom-prosemirror-composer-image img,
.ProseMirror-selectednode.embercom-prosemirror-composer-video iframe,
.ProseMirror-selectednode.embercom-prosemirror-composer-messenger-card
.intercom-interblocks-messenger-card,
.ProseMirror-selectednode.embercom-prosemirror-composer-html-block,
.ProseMirror-selectednode.embercom-prosemirror-composer-button .intercom-h2b-button {
outline: 2px solid #8cf;
}
hr.ProseMirror-selectednode,
.embercom-prosemirror-composer-template.ProseMirror-selectednode,
.embercom-prosemirror-composer-mention.ProseMirror-selectednode {
outline: 1px solid #8cf;
}
</style>
<div>
<!----><div contenteditable="true" role="textbox" dir="auto" data-insertable="true" class="ProseMirror embercom-prosemirror-composer-editor dir-auto"><p class="intercom-interblocks-align-left embercom-prosemirror-composer-block-selected" style="">Hey!Hey!Hey!Hey!Hey!</p><p class="intercom-interblocks-align-left" style=""><br></p></div></div>
<div class="flex flex-row flex-wrap gap-4 embercom-prosemirror-composer-attachment-list">
<!----></div>
<!---->
<!---->
<!---->
<!---->
<!---->
<div></div>
<!---->
<!----></div>
<!---->
<!----></div></div>
If you want to use ember here is a possible solution:
hey = driver.find_element_by_xpath('//*[contains(#id="ember")]/div/div[3]/div[1]/div/p')
hey.send_keys("Hey!")
This will probably fail if there are multiple elements with id="ember[0-9]+".
If you want to access the p tag directly use find_element_by_css_selector, like so:
hey = driver.find_element_by_css_selector('.intercom-interblocks-align-left.embercom-prosemirror-composer-block-selected')
hey.send_keys("Hey!")
Your code with find_element_by_class_name did not work because it's expecting one class name and you are passing two class names (class names are separated by space).
This is the html code:
<div id="dcf-page-export-buttons" class="no-print" style="display: block;">
<a id="dcf-saveaspdf" href="#" target="_blank" class="k-button">
Save as PDF
</a>
<a id="dcf-saveaspng" href="#" target="_blank" class="k-button">
Save as Image
</a>
<a id="dcf-printPdf" class="k-button" href="#">
Print
</a>
<a id="dcf-btnClose" class="k-button" href="#">
Close
</a>
</div>
I want to click on the Print href but it isn't being clicked. Here is my code:
exportLink = driver.find_element_by_link_text("Export")
exportLink.click()
print = driver.find_element_by_id("dcf-printPdf")
print.click()
Before finding element by id for print, I had clicked on Export href which opened a new tab and after opening of the new tab, I'm trying to click on print but getting an error. This is the error:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Let me know if I'm going wrong somewhere or if there is a problem in the html.
The first part of the question has been answered. This is the 2nd part:
On clicking on this Print button:
This window gets opened. It's not a new tab per se, but just a new window. Within that window, I want to click on the Save button. Is there a way to go about doing that? This is how the view looks like:
And here is the html code.
<cr-button class="action-button" aria-disabled="false" role="button" tabindex="0">
Save
</cr-button>
Here is my code till now:
exportLink = driver.find_element_by_link_text("Export")
exportLink.click()
driver.switch_to.window(driver.window_handles[1])
driver.execute_script("document.getElementById('dcf-user-info').style.display = 'none';")
time.sleep(1)
print = driver.find_element_by_link_text("Print")
print.click()
This is the snip of the error log. I've added the snip because I'm unsure of the error.
Small continuation of the error:
You need to switch to new tab first then you can click on Print using that Id that you have been trying.
Switch to new windows like this :
driver.switch_to.window(driver.window_handles[1])
As you can see from the error, the element you trying to access is not interactable.
So, the problem is not with HTML.
I can't see the page you working on, but the problem can be that element you trying to access is out of the view.
Try this
from selenium.webdriver.common.action_chains import ActionChains
print = driver.find_element_by_id("dcf-printPdf")
ActionChains(driver).move_to_element(print).click(button).perform()
I am writing a program to gather data from a website, however at some point in the program I need to enter an email and password into text boxes. The route I am attempting to go is to use driver.execute_script to change the underlying HTML, however I am having difficulty connecting that command with the XPATH value that I use to find the element. I know there are a few threads on here that deal with similar issues, however I have been completely unable to find one that uses XPATH. Any help would be greatly appreciated as I am totally stuck here.
Below is the line of HTML that I am attempting to change, as well as the XPATH value associated with the text box.
XPATH Value
/html/body/center/div[4]/table[2]/tbody/tr/td[4]/table[1]/tbody/tr[2]/td/table/tbody/tr[7]/td[2]/font
HTML:
<input onclick="if (this.value == 'Enter Your Name') this.value='';" onchange="if (this.value == 'Enter Your Name') this.value='';" name="name" type="text" value="Enter Your Name" style="padding-top: 2px; padding-bottom: 6px; padding-left: 4px; padding-right: 4px; width:120px; height:15px; font-size:13px; color: #000000; font-family:Trebuchet MS; background:#FFFFFF; border:1px solid #000000;">
I am attempting to replace value = "Enter Your Name" with value = "Andrew" - or any other name for that matter. Thank you very much for any and all advice, and please let me know there is any additional data / info that is required.
Send_Keys scripts:
name = driver.find_element_by_xpath('//body/center/form/span/table/tbody/tr/td[1]/input')
name.clear()
name.send_keys("Andrew")
Your <input> tag is contained within an <iframe>, so you'll need to switch the context to the <iframe> first:
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
Now that you're "inside" the <iframe>, your send_keys script should work:
name = driver.find_element_by_xpath('//body/center/form/span/table/tbody/tr/td[1]/input')
name.clear()
name.send_keys("Andrew")
Lastly, here's how to switch back to the default content (out of the <iframe>):
driver.switch_to.default_content()
Hi I have the following code
<md-option ng-repeat="sector in allSector | orderBy:'name'" ng-value="{"id":239,"name":"MU-BHND-JC32-0033","regionInventory":{"id":18,"regionName":"Mumbai","zone":{"id":4,"name":"WEST","longitude":73.1812,"country":{"id":3,"name":"INDIA","countryCode":"IND"},"latitude":22.3072},"stateCode":"MU","latitute":19.076,"longitude":72.8777},"latitute":19.29117616,"longitude":73.04375901,"coordinates":null,"centroid":null}" tabindex="0" class="ng-scope md-ink-ripple" role="option" aria-selected="false" id="select_option_982" aria-checked="true" value="[object Object]" style>
Basically I want to select the element MU-BHND-JC32-0033 from the above code and click on it.
As id keeps on changing I am not open for going for id.
if someone can help me out in clicking using xpath and ng-value.
I tried the following but it failed
subRegOpt = driver.find_element_by_xpath("//md-option[#ng-value='MU-BHND-JC32-0033']")
sleep(5)
subRegOpt.click()
Try to replace
subRegOpt = driver.find_element_by_xpath("//md-option[#ng-value='MU-BHND-JC32-0033']")
with
subRegOpt = driver.find_element_by_xpath("//md-option[contains(#ng-value, 'MU-BHND-JC32-0033')]")
Currently I have this input
<label class="bold grey-rectangle" data-uploading="Uploading" data-completed="Upload completed">
<span>+ Add certificate</span>
<input type="file" data-validation-allowing="jpg, jpeg, pdf" data-validation="mime" name="qualification2">
<div class="spinner hidden">
<div class="spinner-inner"></div>
</div>
</label>
and I want to send a file to the same, so I'm running the following codes:
self.driver.execute_script("document.getElementsByName('qualification2')[0].style.display='block';")
self.driver.find_element_by_name("qualification2").send_keys("certificate.jpeg")
but I'm getting the following error
ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
How, effectively, I can do an upload with python and selenium?
You should try removing the hide class to make the element visible:
elm = self.driver.find_element_by_name("qualification2")
self.driver.execute_script('arguments[0].removeAttribute("class");', elm)
elm.send_keys("/absolute/path/to/certificate.jpeg")
Used the link you've provided, tested it:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://your/site")
elm = driver.find_element_by_name("qualification2")
driver.execute_script('arguments[0].removeAttribute("class");', elm)
elm.send_keys("/Users/user/Downloads/test.jpg")
Produced:
Note that in Firefox you would also have to reset the margin-left style property to 0 to make the element really visible:
driver.execute_script('arguments[0].removeAttribute("class"); arguments[0].style["margin-left"] = 0;', elm)