I'm trying to make a Instagram bot that logs in, opens your profile etc. Everything works fine until one anchor tag:
self.driver.find_element_by_xpath("/html/body/div[1]/section/main/div/ul/li[3]/a").click()
I also tried the shorter way of writing this:
self.driver.find_element_by_xpath("//a[contains(#href,'/Obserwowani:')]").click()
and also tried searching by id.
This is a snippet of my function:
self.driver.find_element_by_xpath("/html/body/div[1]/section/main/section/div[3]/div[1]/div/div/div[2]/div[1]/div/div/a").click()
sleep(15)
self.driver.find_element_by_id("/html/body/div[1]/section/main/div/ul/li[3]/a").click()#not working
sleep(4)
scrollbox = self.driver.find_element_by_xpath("/html/body/div[5]/div/div/div[2]"
Can somebody tell me what is wrong please? Even sleep() doesn't work.
Related
Apologies if this question was answered before, I want to click on an area in a browser with plain text using Selenium Webdriver in python
The code I'm using is:
element_plainText = driver.find_elements(By.XPATH, '//*[contains(#class, "WgFkxc")]')
element_plainText.click()
However this is returning "ElementNotInteractableException". Can anyone help me out with this?
Selenium is trying to be helpful here, by telling you why it won't click on the element; ElementNotInteractableException means it thinks that what you're trying to click on isn't clickable.
This usually happens because either:
The element isn't actually visible, or is disabled
Another element is "overlapping" the element, possibly invisibly
You're clicking something Selenium thinks won't do anything, like plain text
There's two things I'd try to get around this. Firstly, Actions. Selenium has an Action API you can use to cause specific UI events to occur. I'd suggest finding the co-ordinates of the text, then making Selenium click those co-ordinates instead of telling it to click the element. Read more about that API here.
Secondly, try clicking it with Javascript, using a Javascript Executor. That can often give you the same outcome as using Selenium directly, without it being so "helpful".
I'm working on a personal project to automate the joining of a Twitch channel through Selenium using Python. This is my first time using Selenium and I've run into an issue that I can't seem to resolve.
When joining a channel that is flagged as containing mature content, it requires a user to click a button to start watching the stream. My program works great until the button needs to be clicked.
The HTML for button appears as:
<button class="ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jnsGXs ksFrFH" data-a-target="player-overlay-mature-accept"><div class="ScCoreButtonLabel-sc-lh1yxp-0 evtFDZ"><div data-a-target="tw-core-button-label-text" class="Layout-sc-nxg1ff-0 eZactg">Start Watching</div></div></button>
Since the class name appear to be dynamically generated, I planned on the using the "data-a-target" as the selector. Looking at Selenium's docs, it appeared that the find_element_by_css_selector function is what I need to use. I played around in the Chrome developer tools and was about to select the button using: document.querySelector('button[data-a-target="player-overlay-mature-accept"]'), so I was confident that it would work with Selenium.
I made the selection in my code with Selenium using the following code:
mature = driver.find_element_by_css_selector('button[data-a-target="player-overlay-mature-accept"]')
Unfortunately, Selenium doesn't seem to be able to find it and returns the error message:
Message: no such element: Unable to locate element: {"method":"css selector","selector":"button[data-a-target="player-overlay-mature-accept"]"}
(Session info: chrome=97.0.4692.71)
Am I doing something blatantly wrong? I've spent a good amount of time trying different things but nothing seems to work.
Maybe this:
mature = driver.find_element_by_xpath("//button[#data-a-target='player-overlay-mature-accept']")
or
mature = driver.find_element_by_xpath("//button[contains(#data-a-target, 'mature')]")
I am not completely new to selenium but I cannot work this out. I am supposed to do a basket automation and I have troubles at the first page.
I am supposed to fill three input boxes at first to proceed to the actual order but I fail at the first input. We have unique ids for almost every input so it should not be too hard to find it but somehow it is.
<input class="form-input--text" data-validitytext-empty="Položka je povinná.<br>Prosíme, vyplňte ji." data-validitytext-invalid="Položka má špatný formát.<br>Prosíme, opravte ji." data-label-selector="closest(.form-item--required)->find(label.form-label)" id="surname003" name="Prijmeni" type="text" value="" data-form-required="true" xpath="1">
This is how the first input is described with id of "surname003" so my first try was to do this
driver.find_element_by_xpath("//*[#id='surname003']").send_keys("text")
Then I tried to access it with id
driver.find_element_by_id("surname003").send_keys("text")
Nothing from this was working so I tried to get the full xpath of it but that did not work.
This is how the code looks as a whole
driver = webdriver.Chrome(executable_path=r"C:\Users\KDK\Desktop\Selenium setup\chromedriver.exe")
driver.get(URL2)
driver.maximize_window()
driver.implicitly_wait(10)
driver.find_element_by_xpath("//*[#id='surname003']").send_keys("test")
driver.find_element_by_xpath("//*[#id='phone03']").send_keys(telefon)
driver.find_element_by_xpath("//*[#id='email03']").send_keys(email)
This is the error I get
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
This is the page I am working with :
https://www.nev-dama.cz/zima/rakousko/lungau/residence-carpe-solem-mariapfarr/rezervaceNova?termin=2021-01-10&typologie=[26225]&delka=3
I am thinking if the issue of locating the element is somehow connected to the fact that you can switch between two "windows" in the reservation page. You can switch between "Not binding reservation" and "Buy online" and I dont know if that makes it somehow harder to get to the element.
I hope someone can help me with this. If something is not clear please ask me and thanks for reading and thanks in advance for some answers! :)
They have few inputs with the same id and first is from another from. But it mustn't raise NoSuchElementException.
Try XPATH like that
//div[#id="koupit-online"]//*[#id='surname003'].
You need to add this part //div[#id="koupit-online"] to all all xpath or use as context
form = driver.find_element_by_xpath("//div[#id='koupit-online']")
form.find_element_by_xpath(".//input[#id='surname003']").send_keys("test")
form.find_element_by_xpath(".//input[#id='phone03']").send_keys(telefon)
form.find_element_by_xpath(".//input[#id='email03']").send_keys(email)
Second problem can be chromedriver. When i used chrome with selenium i got this exception without reason. I changed browser to firefox and geckodriver and this never happend again
I am trying to use Selenium with Python to click on a text field, which opens a pop-up panel, select the text entry area of that popup, and enter text to it.
switch_to_window and switch_to_frame don't seem to be working. In a previous question I asked about Selenium, someone told me to pause the program until the element I need is available. The solution worked for that problem, but not this one, so I'm assuming I have a different issue and I'm too new to Selenium to understand what it is.
This is what the original box I'm trying to click on looks like:
And the Inspect Element for this box:
When that description box is clicked, it should open this window:
And select this element to enter text into:
So in my code I have:
descriptionBox = driver.find_element_by_id('kiadvany_fulszoveg_text')
descriptionBox.click()
That does not error the program, but it also doesn't seem to actually be clicking on that element. To make matters more confusing, I got this to work exactly ONCE, where it opened the correct Description text box as pictured above, but it has since not worked at all even when I try the exact same thing.
The panel's ID is:
As I mentioned, switching to this panel ID using switch_to_frame or switch_to_window was the first thing I tried, but I'm getting a No Such Element error.
Because I saw the description box open correctly once, but never again, I'm assuming that's where the problem is. I wish, the one time it did pop up, that I'd tried to put the text into the field to see if that would work too, but I hadn't gotten there yet at that point, so I don't know if that would have worked.
Thank you in advance to anyone who can help with this!
Try this
descriptionBox = driver.find_element_by_id('kiadvany_fulszoveg_text')
driver.execute_script('arguments[0].click();', descriptionBox)
or
actions = ActionChains(driver)
actions.move_to_element(descriptionBox)
actions.click(descriptionBox)
actions.perform()
As the title says, how can I .click() a button using Selenium, when the button gets "disabled" after using the method clear or send_keys?
Before:
That's the page status when I open it's url... but then right after I run my code to find the textbox and replace it's value, the element gets disabled (maybe by some sort of JS) right after I clear it's content or write something to it using send_keys.
After:
Code:
txt_value = driver.find_element_by_xpath('//input[#id="txtValor4"]')
txt_value.clear() #this disables the button
txt_value.send_keys(str(123,45)) #this also disables the button
My question is:
How can I bypass this website protection and press the Continuar button?
I thought about disabling JS, but the whole website relies on it to produces the requires documents.. wrong alternative.
So I thought about using the button properties to simulate the pressing of the button... just don't know if it's possible, or how I could do this.
Another option was blocking only the JS that disables the button maybe mapping where the command comes from using the inspect element and network tools...
So is there any way to achieve this?
ps.: I can't give the URL because it requires my login data.
Ok, so you can't directly do this through normal means. Selenium WebDriver is made to simulate real use of a browser. It may be possible however to use the execute_script function. (Java Selenium has a built in JavascriptExecutor class, I assume this is python's version.) The execute_script function allows Selenium to perform tasks that a human interacting with a browser can't do.
driver.execute_script("document.getElementById('buttonid').click()")
Or something along those lines should work. Hope that helps you out.
If you don't get any solution with selenium and javascript, you can use Sikuli concept. To click that element, take the image of the 'Continuar' button and save it in resources folder.
String elementImg=Path of the Image;
screen.click(elementImg);
I could bypass this using driver.execute_script("document.getElementById('txtValor4').value = 123.45"), to pass the values into the textbox, so the button didn't got disabled and I could press the Continue button.
Even bypassing this, the result wasn't the expected! The value that I entered was supposed to be corrected by some sort of interest. But bypassing this, the value isn't corrected.
Today the user that asked the program told me that everytime I change the value inside this textbox, I must press the Calculate button.
So, instead of inefficiently bypassing this disable method, I could solve my problem using:
b = driver.find_element_by_xpath('//input[#id="txtValor4"]')
b.clear()
b.send_keys('123.45')
driver.find_element_by_xpath('//input[#id="btnCalcular4"]').click()
driver.find_element_by_xpath('//input[#id="btnContinuar4"]').click()
This way the tax value is corrected by interest and the website generate the .pdf with the exact value that I was expecting.
Many thanks for everyone that put some time and effort trying to help me.