I have a page where I can't click on a button with selenium the html look like this before clicking into button:
And that's how is look like after pressing the button:
Is have a way to insert that values into the html tag to get visibility of the element I want in the case button.I try with WebDriverWait and ActionChains but no success.
I execute the javascript code:
driver.execute_script('document.getElementsByTagName("html")[0].setAttribute("data-whatelement", "button")')
driver.execute_script('document.getElementsByTagName("html")[0].setAttribute("data-whatclasses", "Button__StyledButton-iESSlv,dJJJCD Button-dtUzzq kHUYTy")')
And still the button element is not visible.How can I get the element try with EC wait and action chain.Is it timeout exception.Any advice.
Using execute_script you can run javascript and manipulate the DOM at will. See: Running javascript in Selenium using Python
So, for example (I'm not entirely sure what you need to do to get visibility of the element), to re-insert the data-inq-observer attribute into the body tag:
driver.execute_script('document.getElementByTagName("body").setAttribute("data-inq-observer", "1")')
Related
I am trying to get data from a webpage (https://www.educacion.gob.es/teseo/irGestionarConsulta.do). It has a captcha at the entry which I manually solve and move to the results page.
It happens that going back from the results page to the entry page does not modify the captcha if I reach the initial page with the "go back" button of the browser; but if I use the driver.back() instruction of Selenium's WebDriver, sometimes the captcha is modified - which I'd better avoid.
Clearly: I want to get access from Selenium to the DOM window (the browser), rather than to the document (or any element within the html) and send the ALT+ARROW_LEFT keys to the browser (the window).
This, apparently, cannot be done with:
from selenium.webdriver import Firefox
from selenium.webdriver.common.keys import Keys
driver = Firefox()
driver.get(url)
xpath = ???
driver.find_element_by_xpath(xpath).send_keys(Keys.ALT, Keys.ARROW_LEFT)
because send_keys connects to the element on focus, and my target is the DOM window, not any particular element of the document.
I have also tried with ActionChains:
from selenium.webdriver.common.action_chains import ActionChains
action = ActionChains(driver)
action.key_down(Keys.LEFT_ALT).key_down(Keys.ARROW_LEFT).send_keys().key_up(Keys.LEFT_ALT).key_up(Keys.ARROW_LEFT)
action.perform()
This also does not work (I have tried with several combinations). The documentation states that key_down/up require an element to send keys, which if None (the default) is the current focused element. So again here, there is the issue of how to focus on the window (the browser).
I have thought about using the mouse controls, but I assume it will be the same: I know how to make the mouse reach any element in the document, but I want to reach the window/browser.
I have thought of identifying the target element through the driver.current_window_handle, but also fruitlessly.
Can this be done from Selenium? If so, how? Can other libraries do it, perhaps pyppeteer or playwright?
Try with JavaScriptExecutor - driver.execute_script("window.history.go(-1)")
I am trying to automate bootstrap carousel next button with Selenium.
The code is given as follows:
from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path='d:/CRD/chromedriver_win32/chromedriver.exe')
driver.get('file:///D:/slider/bootst.html')
button1 = driver.find_element(By.CLASS_NAME, "a.carousel-control")
button1.click()
The way I am trying to get the ID using chrome inspect element option as shown below:
How can I get the next button pressed automatically using Selenium.
You can use other selectors like XPATH if you want, but I think you should use CSS SELECTOR for "a.right.carousel-control" not CLASS_NAME.
More explanation on stackoverflow
Have a great day !
I am having some issues with Selenium not clicking the pop-up privacy button on https://www.transfermarkt.com/
Here is my code so far:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
It comes up saying:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div[2]/div[3]/div[2]/button"}
Anyone have any thoughts?
I believe the issue is that the element you are trying to click is inside an iframe. In order to click that element you'll have to first switch to that frame. I noticed the iframe has title="SP Consent Message" so I'll use a CSS Selector to identify it based on that. Your code with the added line:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
driver.switch_to.frame(driver.find_element_by_css_selector('iframe[title="SP Consent Message"]'))
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
Note that you may have to switch back to the default frame to continue your test, I'm not 100% sure as that iframe has gone away.
Also it seems like some folks don't get that popup when the hit the website, not sure why, may be something you want to look in to to determine how you want to test this.
Like #Prophet says you should improve the xpath for the button (again it seems to have a unique title so I would use CSS Selector 'button[title="ACCEPT ALL"]'), but this works for me to click it.
There are 2 issues here:
You need to add wait / delay before accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button') to let the page load
It's very, very bad practice to use absolute XPaths. You have to define an unique, short and clear related XPath expression.
Additionally, I see no pop-up privacy button when I open that page. So, maybe that element is indeed not there.
I am using Python script to open a web page, then a tab and then i want to click on a button. I am stuck on the last part. I am unable to click the find button. Here is the HTML code when i am using inspect in chrome.
input value="Find" class="cuesButton" name="findButton"
onclick="javascript:onFindSubmit()" type="button"
Here is the button i am trying to click:
I tried driver.find_element_by_name, element_by_id. It says method css selector doesnt have this element by name but it still fails.
2 things can be happening, 1 the page isn’t finished loading, to wait use WebDriverWait like so:
wait = WebDriverWait(driver, 10)
Or 2, the element is in an iframe , in this case you would need to switch to it like so:
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
You can switch using xpath, name, cssselector, and tag name
I'm trying to click the next button on a webpage multiple times, I need scrape the page after each click. The following code is shortened but illustrates my situation. I am able to scrape the required tag after the first click, but then the second click fails. Here is the code
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
browser.get('http://www.agoda.com/the-coast-resort-koh-phangan/hotel/koh-phangan-th.html')
browser.find_element_by_xpath("//a[#id='next-page']").click()
browser.find_element_by_xpath("//a[#id='next-page']").click()
browser.quit()
I get this error
WebDriverException: Message: Element is not clickable at point
Although when I use beautifulSoup and get the source code after the first click the element I am trying to click again is clearly there to click.
I've read on other answers I might need to wait some time but not sure how to implement that.
The first time you click the next page starts to load, you can't click on a link that is on a page that is still loading or on a link on a page that is about to be replaced by another page! So the message is quite correct. So you just have to wait for the next page to appear before you can click on the next link. You can just sleep for N seconds or, much better, wait for the element to appear before clicking on it again.
Have a look at: Expected Conditions
So there you would probably want to use element_to_be_clickable on each attempt to click. What happens is it waits N seconds for it to be clickable and if it does not take that state in that time it throws an exception.
Here is a good blog post on all this: How to get Selenium to wait for page load after a click
Here is a simple example of how you can use that. Here you have just clicked on the link for the first time, and then after that to wait for the next to load you can:
xpath = "//a[#id='next-page']"
WebDriverWait(browser.webdriver, 10).until(
expected_conditions.element_to_be_clickable((By.XPATH, xpath)))
browser.find_element_by_xpath(xpath).click()