I was trying to find out a dynamic element of a button from a html page with selenium
The syntax is given below
toggle=driver.find_element_by_xpath'//*[#id="__layout"]/div/div/nav/div/div[2]')
but I'm getting a syntax error as an output.
I'm using python and google chrome driver in this purpose. Also is there a better way to find dynamic elements, specially button elements?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver=webdriver.Chrome('C:/Users/Super User/PycharmProjects/webautomation/chromedriver.exe')
driver.get("abcd")
toggle=driver.find_element_by_xpath('//*[#id="__layout"]/div/div/nav/div/div[2]')
toggle.click()
AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'
Yup this is the problem now the complete code is given, and I want to click the button by the "xpath" or any other way.
Thanks for your quick response
Related
I was using Selenium find_element_by_xpath to click the cookie popup "Accept" button. But it was failing to find the button. So, I tried to locate the button using querySelector in the dev console. Also, the querySelector failed to find the button but after clicking inspect on the button the querySelector able to find the button.
Also, searching the xpath in dev elements just showing 1 of 1.
Why this is happening ? How do I click the "Accept" button using selenium ?
Website link: https://www.transfermarkt.com/
The xpath: //*[#id="notice"]/div[3]/div[2]/button
After inspect on the button.
That element is inside an iframe, so to access it you will first have to switch to that iframe.
You didn't mention what language are you using so I will give my solution in Python. This can be done with other languages as well with some little syntax changes.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,'iframe[title="SP Consent Message"]')))
wait.until(EC.visibility_of_element_located((By.XPATH, '//button[#title="ACCEPT ALL"]'))).click()
When finished working inside this iframe you will have to switch back to the default content with
driver.switch_to.default_content()
I am working with Selenium Webdriver with Java.
And I was trying to interact with anchor tag which is enclosed as pseudo element ::before
But I am unable to interact with anchor element.
Here is the screenshot of the HTML structure.
With
JavaScriptExecutor, I understand, we can fetch the propertyValue using window.getComputedStyle().getPropertyValue() but I am not sure, how to interact with <a> element and execute a Click.
Initially, I attempted to click on the Anchor Element without considering the pseudo element as simple Element Interaction.
To fetch the Element:
private By tabRawView_By_CSS = By.cssSelector("[tabid='raw-view'][role='tab']");
But this piece is not throwing any error but it is also not clicking on the element.
Then I thought of using JavaScriptExecutor and was trying to run first in Developers Tool as below image but couldn't find suitable options.
Can anyone please suggest?
If it is Java bindings and all you want to do is to click on an achor tag which has Raw view as a text.
You could try with ExplicitWaits :
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Raw View"))).click();
It is the line of code that i am using to find the element using chrome web driver and python
This the error that is thrown everytime when i try to run the code
These are the buttons on the header bar and I am trying that my code should click the html button
This is the html code for the efforts button which i am trying to target using selenium so that it can click here but it always says no such element found
I have even created a seperate function to again and again find the element and sleep for 5 sec after every successful attempt until it has clicked on the element but still no luck it runs on forever without ever finding the element
This is where i am calling the function explained in the 5th picture. Here driver is chromedriver and it keeps on repeating for 10 times with 5 sec intervals but still no luck and i am continuously watching the chrome window where driver do all the work and the page has loaded but even after it is loaded it is still not working.
It is the beginning of entire html code from inspect element in case needed by someone
This is the remaining HTML code with EFFORTS area selected in case needed
I have even tried using shadow root but that is also not working and i am also not sure that this is a case of shadow root as i have not seen shadow root specified in html when i checked the html code.
The element you are trying to locate is under iframe with class iframeStyle. First switch to that iframe to access the Efforts link.
try below xpath using xpath contains to resolve your issue:
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//a[contains(text(), 'EFFORTS')]")))
or
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.ID, "tmsMobileId")))
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
If you still facing no such element exception then please check if your element is present under iframe. If so then you need to switch iframe before handling EFFORTS anchor tag
Is there a good method to find the xpath of an element that only appears very briefly (e.g. for a second or two, like a loading screen).
If I click within an application and a short loading screen appears I would like to find the xpath of the loading element so I can have selenium wait until the element is no longer on the page before continuing.
Sometimes the loading screen appears over the element I want to click and catches the click instead.
Thanks!
Assuming that you are struggling to find XPath of the element that quickly disappears from the page before you could inspect and find the xpath.
You can try opening the console in the webpage by using Ctrl+Shift+I.
Then navigate to the Network tab next to console tab
Below Network tab you will find Online dropdown.
Click on the arrow beside the online dropdown, you will find multiple options click on the one which says Slowest This will reduce the speed of your website loading and gain you more time to find the xpath.
or
You can also customize the throttle by clicking on add option and providing Download, Upload and Latency. or you can directly choose Offline option once your loading icon is enabled.
Attached screenshot for your reference.
Hope this helps.
You can try something like below using web driver wait, not sure which programing language you are using butyou refer same concept with other languages too:
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'your text1')]")))
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
The html element Im trying to locate is the "Shared" link.
I wrote a dynamic xpath to locate the element and it shows as the element was identified in the developer console.
But when i use the xpath that i wrote in the developer console to locate the element using selenium, it does not locate the element.
The method i used to check if it locates the element is shown below.
I could not figure out why this issue occurs, Is it because of a issue in the xpath that i have written or because of another issue?
Code you can try out is :
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath(" your Xpath ")));
driver.findElement(By.xpath("your Xpath")).click();
The Xpath you have written would work if only one title is present on current page.
driver.find_elements doesn't have attribute to click() so use driver.findElement instead of Elements