我正试图摆脱首次访问此页面时显示的弹出窗口:https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10
根据html代码,我希望Selenium“点击”的“ x”元素的ID为“ leadpages-close-button”
所以我试图用以下代码单击它:
function Get-SonarQubeStatus() {
# Step 1. Create a username:password pair
$credPair = "username:password"
# Step 2. Encode the pair to Base64 string
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credPair))
# Step 3. Form the header and add the Authorization attribute to it
$headers = @{ Authorization = "Basic $encodedCredentials" }
# Step 4. Make the GET request
$responseData = Invoke-WebRequest -Uri https://localhost/api/qualitygates/project_status?projectKey=<projectkey> -Method Get -Headers $headers -UseBasicParsing
#write-host $responseData.content
$x = $responseData.content | ConvertFrom-Json
$sonarQualityGateResult = $x.projectStatus.status
if($sonarQualityGateResult -eq "ERROR")
{
write-host "CI failed due to Sonarqube quality Gate"
exit 1
}
}
但是会导致错误:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Safari()
driver.get("https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10")
driver.find_element_by_id("leadpages-close-button").close()
我在做什么错了?
答案 0 :(得分:2)
该元素包含在iframe中,因此您必须先切换到iframe。试试:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Safari()
driver.get("https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-1")
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_tag_name("iframe")))
#driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) # OR driver.switch_to.frame(0) # switch by iframe index
WebDriverWait(driver,20).until(EC.element_to_be_clickable(driver.find_element_by_id("leadpages-close-button")))
driver.find_element_by_id("leadpages-close-button").click()
driver.switch_to.default_content()
答案 1 :(得分:1)
尝试以下代码以切换到动态iframe,然后单击“关闭”按钮
<div class="test">HelloWorld!</div>
<div class="test">Myname</div>
<div class="test">sometext!</div>
答案 2 :(得分:1)
要关闭首次访问网址https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10
时显示的弹出窗口,因为弹出窗口位于<iframe>
内:
click()
。代码块:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[contains(@src,'https://realestatekh.lpages.co/leadbox')]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[contains(@action,'https://realestatekh.activehosted.com/proc.php')]/a[@id='leadpages-close-button']"))).click()
浏览器快照:
答案 3 :(得分:-1)
尝试:
driver.get("https://www.realestate.com.kh/buy/?listing_type=sale&categories=Condo&order_by=relevance&is_certified=1&private_seller=1#page-10")
time.sleep(1)
driver.find_element_by_id("leadpages-close-button").close()