Here is my python code
elements=driver.find_elements_by_id("folder_c0fb056b-f83a-495e-9db1-dd1b19942eaa")
print(elements)
the result is [ ]. The same way has worked for other parts of the website but when I get into a table inside the website, it does not work correctly. I could find and click some links prior to these command lines. Please look at the images I attached. When you look at HTML2, the white box is div "folder_c0fb056b-f83a-495e-9db1-dd1b19942eaa" shown in the last attached image (Webpage view). The rest blue boxes are the regions I am interested in. It looks like
<a id="rptReportFolders_ctl04_rptReports_ctl00_hlReportNameDescription" href="/da2/Reports/ReportResults.aspx?ReportID={b3583c5f-874b-4eca-9548-bf88f99ff7e6}">
<b>AE-CR Log - Open Items / All Projects</b>
</a>
If you are sure, that id of the elements is not generic you can try to use this:
elements = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//div[#id='folder_c0fb056b-f83a-495e-9db1-dd1b19942eaa']/div/a")))
for element in elements:
print(element.text)
if element.text == 'your text':
element.click()
The main idea of this code snippet is to use WebDriverWait to wait at least 10 seconds until all elements will be present on the page and only then print the list of them. Also I have proposed another selector which will locate all a links.
Note: you have to add some imports:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
Related
There is a div that is always present on the page but doesn't contain anything until a button is pressed.
Is there a way to wait until the div contains text, when it didn't before, and then get the text?
WebDriverWait expected_conditions provides text_to_be_present_in_element condition for that.
The syntax is as following:
wait.until(EC.text_to_be_present_in_element((By.ID, "modalUserEmail"), "expected_text"))
Afte using the following imports and initializations:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
Used here locator By.ID, "modalUserEmail" is for example only. You will need to use proper locator matching relevant web element.
UPD
In case that element has no text content before the action and will have some text content you can wait for the following XPath condition:
wait.until(EC.presence_of_element_located((By.XPATH, "//div[#class='unique_class'][text()]")))
//div[#class='unique_class'] here is unique locator for that element and [text()] means that element has some text content.
UPD2
If you want to get the text in that element you can apply .text on the returned web element. Also, in this case it is better to use visibility_of_element_located, not just presence_of_element_located, so your code can be as following:
the_text_value = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[#class='unique_class'][text()]"))).text
I'm having an issue clicking an icon/link in a table. I've tried both find_element_by_xpath and find_elements_by_Xpath - no luck with either. I forced the wait as I was getting some issues with the element not being found.
I've highlighted the icon in the Table row, with the red box. in this image.
Website
Also found the Xpath but can't seem to get it to work, the icon is clickable on the webpage.
Xpath
My code is below:
driver.implicitly_wait(7)
tr = driver.find_element_by_xpath('//*[#id="AthleteTheme_wt6_block_wtMainContent_wt9_wtClassTable_ctl05_AthleteTheme_wt221_block_wtIconSvg_Svg"]/svg/use')
tr.click()
Thanks
The Element is in an svg tag. And there is a different syntax for the same. Links to refer - Link1, Link2
To access svg tag elements the syntax would something like this:
//*[local-name()='svg']
As per the screen shot the xpath for the Elements would be:
//span[#id="AthleteTheme_wt6_block_wtMainContent_wt9_wtClassTable_ctl05_AthleteTheme_wt221_block_wtIconSvg_Svg"]/*[local-name()='svg']/*[local-name()='use']
You can not directly use // to locate an SVG element. They are one of the special tags.
Always use //*[name()='svg'] or //*[local-name()='svg'] to locate them.
Based on the HTML that you've shared, Please use the below xpath :
//a[#class='svgContainer']//child::span//*[name()='svg' and starts-with(#id,'AthleteTheme')]//*[name()='use']
** Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
Code trial 1 :
time.sleep(5)
driver.find_element_by_xpath("//a[#class='svgContainer']//child::span//*[name()='svg' and starts-with(#id,'AthleteTheme')]//*[name()='use']").click()
Code trial 2 :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='svgContainer']//child::span//*[name()='svg' and starts-with(#id,'AthleteTheme')]//*[name()='use']"))).click()
Code trial 3 :
time.sleep(5)
button = driver.find_element_by_xpath("//a[#class='svgContainer']//child::span//*[name()='svg' and starts-with(#id,'AthleteTheme')]//*[name()='use']")
driver.execute_script("arguments[0].click();", button)
Code trial 4 :
time.sleep(5)
button = driver.find_element_by_xpath("//a[#class='svgContainer']//child::span//*[name()='svg' and starts-with(#id,'AthleteTheme')]//*[name()='use']")
ActionChains(driver).move_to_element(button).click().perform()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
UPDATE:
So thanks to the voted answer it displayed some information not the right information, it shows 0kb out of 100 and when in the inspect element console if doing console.log($0) then the item would be displayed in console how do I fetch this
I want to create a python 3.x programme that gets my stats off of netlify and easybase using selenium. The issue I have come across already is that the element does not have a specific class name and the text widget isn't just a tag nor a tag. Here is a screenshot of the html of netlify the screenshot, and this is the code that I used
element = driver.find_element_by_name("github")
element.click()
login = driver.find_element_by_name("login")
login.send_keys(email)
password = driver.find_element_by_name("password")
password.send_keys(passwordstr)
loginbtn = driver.find_element_by_name("commit")
loginbtn.click()
getbandwidth = driver.find_element_by_xpath('//*[#id="main"]/div/div[1]/div/section/div/div/div/dl/div/dd')
print(getbandwidth.text)
getbandwidth = driver.find_element_by_xpath("//dd[#class='tw-text-xl tw-mt-4px tw-leading-none']")
You can use this to grab the first xpath with that class. Below does the same but if you want to index other elements with similar classes.
(//dd[#class='tw-text-xl tw-mt-4px tw-leading-none'])[1]
Normally we use webdriver waits to allow for the element to become visible.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
getbandwidth = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH,"//dd[#class='tw-text-xl tw-mt-4px tw-leading-none']")))
When I run the code, the website loads up fine but then it won't click on the button- an error appears saying the element is not interacterble. What do I need to do to click the button? I am relatively new to this and would be grateful for any help.
I have already tried finding it by id and tag.
page = driver.get("https://kenpreston.co.uk/author/")
element = driver.find_element_by_id('mk-button-31')
element.click()
SOLVED:
I used driver.find_element_by_link_text and this worked fine.
I have checked the website and noticed that mk-button-31 is an id for a div tag and inside it there is an a tag. Try getting the url from the a tag and do another driver.get instead of clicking on it.
Also the whole div tag is not clickable so that is why you are getting this error.
Use sleep from time library to be sure page fully loaded
from time import sleep
page = driver.get("https://kenpreston.co.uk/author/")
sleep(2)
element = driver.find_element_by_id('mk-button-31')
element.click()
Looks like your element is not clickable you need to replace this id selector with the css and need to wait for the element before click on it.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
page = driver.get("https://kenpreston.co.uk/author/")
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#mk-button-31 span"))
element.click();
Consider adding Explicit Wait to your script as it might be the case the DOM had finished loading and the button you're looking for is still not there.
The classes you're looking for are:
WebDriverWait
expected_conditions
Suggested code change:
#your other imports here
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
#your other code here
page = driver.get("https://kenpreston.co.uk/author/")
element = WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.ID, "mk-button-31")))
element.click()
More information: How to use Selenium to test web applications using AJAX technology
I'm very new to Selenium.
I'm crawling data from this page. I need to scroll down the page and click on "Load More Arguments" to get more text. This is the location to click on.
<a class="debate-more-btn" href="javascript:void(0);" onclick="loadMoreArguments('15F7E61D-89B8-443A-A21C-13FD5EAA6087');">
Load More Arguments
</a>
I have tried this code but it does not work. Should I need more code to locate to that (I think the 1 has already tell the location to click). Do you have any recommendation? Thank you in advance.
[1] btn_moreDebate = driver.find_elements_by_class_name("debate-more-btn")
[2] btn.click()
Find the link by link text, move to the element and click:
from selenium.webdriver.common.action_chains import ActionChains
link = driver.find_element_by_link_text('Load More Arguments')
ActionChains(browser).move_to_element(link).perform()
link.click()
If you get an exception while finding an element, you may need to use an Explicit Wait:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
link = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Load More Arguments")))
ActionChains(browser).move_to_element(link).perform()
link.click()
If I understand your code correctly, I can see a few things wrong.
1. You're using find_elements_by_class_name. I'd recommend using find_element_by_class_name instead. elements returns a list, which isn't needed in a case where there is only one element.
2. You're using btn_moreDebate as the holder for the results of your find_elements, but then interacting with btn.
You should be able to perform the find and click in one action:
driver.find_element_by_class_name("debate-more-btn").click()