I wrote a code that works perfectly in one website, but in another isn't working.
Basically the problem is finding elements.
Also, tried to find it by Relative XPATH and Absolute XPATH, and still it won't find it.
(On the problematic website)
I would like to minimilize sharing, if anything else would help tell me :)
working element:
<div id="areaAvail_0" class="areaAvail red button" style="top: 141.65px; left: 212.221px;" data-areaindex="0" data-areatype="ReservedSeating" data-areaid="f8c68849-c882-e911-80dd-984be16723b6" data-hasqtip="94">
</div>
problematic element:
<div id="areaAvail_0" class="areaAvail red button" style="top: 213.238px; left: 91.901px;" data-areaindex="0" data-areatype="GeneralAdmission" data-areaid="20d4c178-7539-ed11-83d1-e7ab999ef3a1" data-hasqtip="1" aria-describedby="qtip-1">
</div>
The code:
driver = webdriver.Chrome(service=serv_obj)
wait = WebDriverWait(driver, 300)
driver.get(url)
driver.maximize_window()
list1 = driver.find_elements(By.CSS_SELECTOR, "div.areaAvail[data-areaindex]")
wait.until((EC.element_to_be_clickable((By.CSS_SELECTOR, "div.areaAvail[data-areaindex]"))))
print(len(list1))
when i use the Element_to_be_clickable, it's just "stuck" in that line.
if i use time.sleep(), after the time passes it will print 0 (the problemtic website).
Any ideas or suggestions?
thanks in advance!
So the problem is solved.
It was indeed inside an iframe
in order to click the element inside the iframe, i used these lines:
wait.until((EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#myIframe"))))
wait.until((EC.element_to_be_clickable((By.CSS_SELECTOR, "div.areaAvail[data-areaindex]"))))
thanks everyone!
Related
I am using selenium version 4.0.0, for reference.
I'm trying to click a Vuetify card element, which is acting as a button, but I running into an element not interactable: [object HTMLDivElement] has no size and location or just element not interactable errors. I have been able to solve similar problems with action chains in the past, but it doesn't seem to work with this.
This is the list element the button is contained within:
<li class="lu-li list-item" data-v-711d8d7a="" style="display: flex;">
<div class="addCard v-card v-card--link v-sheet theme--light" data-v-711d8d7a="" tabindex="0" onselectstart="return false;">
::before
<i class="v-icon notranslate btnAdd material-icons theme--light enableIcon" data-v-711d8d7a="" aria-hidden="true">
add
::after
</i>
</div>
</li>
The first things I tried was simply clicking on the element, and then clicking on the <i> element beneath it when that didn't work:
addQLButton = driver.find_element(By.CLASS_NAME, "addCard")
addQLButton.click()
addQLButton = driver.find_element(By.CLASS_NAME, "btnAdd")
addQLButton.click()
I have already tried using WebDriverWait on both the v-card <div> and <i> element to make sure they are available before being clicked, which it passes without any issues:
WebDriverWait(driver, timeout=10).until(lambda d: d.find_element(By.CLASS_NAME, "addCard"))
WebDriverWait(driver, timeout=10).until(lambda d: d.find_element(By.CLASS_NAME, "btnAdd"))
I have already tried using action chains to make sure the elements are visible (this has worked in the past with similar errors), but I still run into the same issue:
addQLButton = driver.find_element(By.CLASS_NAME, "addCard")
actions.move_to_element(addQLButton).perform()
driver.execute_script("arguments[0].click();", addQLButton)
addQLButton = driver.find_element(By.CLASS_NAME, "btnAdd")
actions.move_to_element(addQLButton).perform()
driver.execute_script("arguments[0].click();", addQLButton)
The elements are not in an iframe, and I am sure I have the correct window selected as I am still able to interact with its elements.
I am at a bit of a loss, any help would be much appreciated. I'm happy to answer any clarifying questions if I didn't explain the issue clearly enough.
I managed to get it working with some javascript:
js = "document.querySelector('.btnAdd').click();"
driver.execute_script(js)
I would like to test some actions. In this case, I need to click on the text to get an upload form, but none of the forms below do not lead to the desired result.
This part of HTML-code, where I need to do some actions:
<a href="javascript:;" class="dg-hider st-mb__20 st_add-material__link" onclick="iu.ajax(event,'https://alexanderro.com/ajax/documentAddGsiUpload');">
Choose your category
</a>
<div id="GSIData"></div>
none of these works:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "dg-hider st-mb__20 st_add-material__link"))).click()
driver.find_element_by_class_name("dg-hider st-mb__20 st_add-material__link").click()
driver.find_element_by_id("GSIData").click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "GSIData"))).click()
driver.find_element_by_css_selector("a[onclick*=https://alexanderro.com/ajax/documentAddGsiUpload]").click()
Could you help me?
You need to wait for the element for its presence or visibility:
You can use the below code snippet, it should work
def wait_for_element_to_be_clickable(element):
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, element)))
element = "a.dg-hider.st-mb__20.st_add-material__link"
wait_for_element_to_be_clickable(element)
driver.find_element_by_css_selector(element).click()
print("Clicked")
I can't make a comment in Stack Overflow, so I'll try help you throug here:
Did you try:
driver.find_element_by_css_selector('[class="dg-hider st-mb__20 st_add-material__link"]').click()
or
driver.find_element_by_xpath('"//a[contains(text(), 'Choose your category')]"').click()
hello colleagues a question, how would I click a href="javascript:void(0)" I have been trying to understand the same question from the same forum but I do not try to understand it very well, I await your contributions
the xpath href => //[#id="course-link-_62332_1"] ,
the xpath h4 = //[#id="course-link-_62332_1"]/h4
here photo
enter image description here
You can do
driver.find_element_by_id('course-link-_62332_1').click()
href="javascript:void(0)" is used to make the browser stay on same page when clicked. It might be performing task/event which is defined in JavaScript/Jquery script or so.
Coming to your question you can click on href by this method.
element1 = self.driver.find_element_by_xpath('//[#id="course-link-_62332_1"]')
element2 = self.driver.find_element_by_xpath('//[#id="course-link-_62332_1"]/h4 ')
element1.click()
element2.click()
So I'm very new to python and selenium. I'm writting an scraper to take some balances and download a txt file. So far I've managed to grab the account balances but downloading the txt files have proven to be a difficult task.
This is a sample of the html
<td>
<div id="expoDato_msdd" class="dd noImprimible" style="width: 135px">
<div id="expoDato_title123" class="ddTitle">
<span id="expoDato_arrow" class="arrow" style="background-position: 0pt 0pt"></span>
<span id="expoDato_titletext" class="textTitle">Exportar Datos</span>
</div>
<div id="expoDato_child" class="ddChild" style="width: 133px; z-index: 50">
<a class="enabled" href="/CCOLEmpresasCartolaHistoricaWEB/exportarDatos.do;jsessionid=9817239879882871987129837882222R?tipoExportacion=txt">txt</a>
<a class="enabled" href="/CCOLEmpresasCartolaHistoricaWEB/exportarDatos.do;jsessionid=9817239879882871987129837882222R?tipoExportacion=pdf">PDF</a>
<a class="enabled" href="/CCOLEmpresasCartolaHistoricaWEB/exportarDatos.do;jsessionid=9817239879882871987129837882222R?tipoExportacion=excel">Excel</a>
<a class="modal" href="#info_formatos">InformaciĆ³n Formatos</a>
</div>
</div>
I need to click on the fisrt "a" class=enabled. But i just can't manage to get there by xpath, class or whatever really. Here is the last thing i tried.
#Descarga de Archivos
ddmenu2 = driver.find_element_by_id("expoDato_child")
ddmenu2.find_element_by_css_selector("txt").click()
This is more of the stuff i've already tryed
#TXT = driver.select
#TXT.send_keys(Keys.RETURN)
#ddmenu2 = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div/form/table/tbody/tr[2]/td/div[2]/table/tbody/tr/td[4]/div/div[2]")
#Descarga = ddmenu2.find_element_by_visible_text("txt")
#Descarga.send_keys(Keys.RETURN)
Please i would apreciate your help.
Ps:English is not my native language, so i'm sorry for any confusion.
EDIT:
This was the approach that worked, I'll try your other suggetions to make a more neat code. Also it will only work if the mouse pointer is over the browser windows, it doesn't matter where.
ddmenu2a = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div/form/table/tbody/tr[2]/td/div[2]/table/tbody/tr/td[4]/div/div[1]").click()
ddmenu2b = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div/form/table/tbody/tr[2]/td/div[2]/table/tbody/tr/td[4]/div/div[2]")
ddmenu2c = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div/form/table/tbody/tr[2]/td/div[2]/table/tbody/tr/td[4]/div/div[2]/a[1]").click()
Pretty much brute force, but im getting to like python scripting.
Or simply use CSS to match on the href:
driver.find_element_by_css_selector("div#expoDato_child a.enabled[href*='txt']")
You can get all anchor elements like this:
a_list = driver.find_elements_by_tag_name('a')
this will return a list of elements. you can click on each element:
for a in a_list:
a.click()
driver.back()
or try xpath for each anchor element:
a1 = driver.find_element_by_xpath('//a[#class="enabled"][1]')
a2 = driver.find_element_by_xpath('//a[#class="enabled"][2]')
a3 = driver.find_element_by_xpath('//a[#class="enabled"][3]')
Please let me know if this was helpful
you can directly reach the elements by xpath via text:
driver.find_element_by_xpath("//*[#id='expoDato_child' and contains(., 'txt')]").click()
driver.find_element_by_xpath("//*[#id='expoDato_child' and contains(., 'PDF')]").click()
...
If there is a public link for the page in question that would be helpful.
However, generally, I can think of two methods for this:
If you can discover the direct link you can extract the link text and use pythons' urllib and download the file directly.
or
Use use Seleniums' click function and have it click on the link in the page.
A quick search resulted thusly:
downloading-file-using-selenium
I have a piece of code as follows
<a class="country" href="/es-hn">
Honduras
</a>
and I'm trying to assign to to a variable by doing
el = self.driver.find_element_by_link_text('Honduras')
However whenever I run it I get the following error:
NoSuchElementException: Message: u"Unable to find element with link text == Honduras"
I've seen link_text fuzz up when trying to find a link when it's in block formation like this. I think it has something to do with the tabulation:
<a class="country" href="/es-hn">
[ ]Honduras
</a>
It only seems to work consistently when in line like this:
<a class="country" href="/es-hn">Honduras</a>
Try this:
el = self.driver.find_element_by_css_selector("a.country[href$='es-hn']")
I agree with sircapslot, in this case partial link text would also work:
el = self.driver.find_element_by_partial_link_text('Honduras')
This may occur when selenium is trying to find the link while your application hasn't rendered it yet. So you need to make it wait until the link appears:
browser.implicitly_wait(10) # 10 seconds
el = self.driver.find_element_by_link_text('Honduras')
The implicitly_wait call makes the browser poll until the item is on the page and visible to be interacted with.